• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

[newbie] Null pointer exception ??

 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Devaka Cooray
ExamLab Creator
Marshal
Pie
Posts: 4600
306
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you getting a NullPointerException on the above program?
 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Devaka Cooray wrote:Are you getting a NullPointerException on the above program?

Yes, when running the code, the logic is skipping through to the else clause.

Another question is whether it is possible to delete char[] variable passwd rather than padding it with spaces ' '?
 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try this:



that might work....
 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Janeice DelVecchio wrote:try this:



that might work....


umm...I wanna change the value not read the value

 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought that 11 was the line you were getting your nullpointer on. Sorry...

Which line is the compiler giving you the NullPointer exception?

and I think you can set a character to null by using:

 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Janeice DelVecchio wrote:I thought that 11 was the line you were getting your nullpointer on. Sorry...

Which line is the compiler giving you the NullPointer exception?


 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't usually use console (I'm a scanner junkie)... so I am of no help to you
 
Amarnath Pandi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you using any IDE like eclipse? I think running the same class on a command line terminal shouldn't throw NPE.
 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 (??)

Source code:


Command prompt log

 
Campbell Ritchie
Sheriff
Pie
Posts: 49847
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

 
Campbell Ritchie
Sheriff
Pie
Posts: 49847
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you read the documentation for Console? Also for System#console()? Always read the documentation before using any classes.

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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49847
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jon Camilleri wrote: . . . Thanks, does that mean that if I switch my code as follows I'm checking for a null anyway?

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)).
 
Amarnath Pandi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've compiled the same code successfully.

Try to set the class path.

or

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.

 
Campbell Ritchie
Sheriff
Pie
Posts: 49847
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Disagree. Don't set a classpath.

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.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a little confused about setting the "password" variable to spaces--how does that make anything secure?
 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It removes the characters from memory so other programs can't go and hack the system memory to retrieve the passwords.

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.
 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am doing a small dance right now because, as useless as I have been in this thread, Rob Prime agreed with me.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49847
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's unusual about Rob agreeing with you? Now, if Rob ever agrees with me . . .
 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with people all the time, as long as they are right
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic