Amarnath Pandi wrote:Are you using any IDE like eclipse? I think running the same class on a command line terminal shouldn't throw NPE.
Yes I'm using Eclipse. The NPE is thrown by Java afaik, why should it run on the CLI?
In any case I gave it a try and javac seems to find a problem compiling the code, for some reason (??)
Command prompt log
Campbell Ritchie wrote:You can always use if (foo == null).
In fact cons.equals(null) should never return true if the equals method has been correctly overridden. If cons is null, it will throw an exception, and if cons is not null it will return false.
Thanks, does that mean that if I switch my code as follows I'm checking for a null anyway?
If you open the JVM with the java command, it usually uses the current terminal/command window as a Console. If however you use the javaw command, it opens without a Console, so System.console() returns null and any attempt to use the console will result in a NullPointerException. Most IDEs use javaw to open the JVM, so they do not provide a Console.
You're welcome, and no it doesn't. I have already told you what happens if cons is null, so always use == null or != null (or, more awkward style, !(foo == null)).
Jon Camilleri wrote: . . . Thanks, does that mean that if I switch my code as follows I'm checking for a null anyway?
Try to set the class path.
Try to use the full path before javac.
"c:\Program Files\Java\jdk1.6.0_02\bin\javac.exe" TestPassword.java
If the class TestPassword is in a package then you have to add the package in front of the class name.
"c:\Program Files\Java\jdk1.6.0_02\bin\javac.exe" package\TestPassword.java
If it compilation succeeded then try
"c:\Program Files\Java\jdk1.6.0_02\bin\java.exe" package.TestPassword
One more correction:
passwd.equals("joe") // I hope this code will not serve the purpose.
You are right that there might be a path problem, since there is a "javac is not recognized" error. That particular error is described in the Java™ Tutorials.
As for clearing the array, using Arrays.fill is shorter. And I agree with Janeice; \u0000 or '\0' (or simply 0) is a better option than spaces. Spaces still mean something, whereas the NULL character doesn't. That's also why it's the C string terminator.