my dog learned polymorphism*
The moose likes Beginning Java and the fly likes [newbie] Null pointer exception ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "[newbie] Null pointer exception ??" Watch "[newbie] Null pointer exception ??" New topic
Author

[newbie] Null pointer exception ??

Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660



Jon
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3018
    
  35

Are you getting a NullPointerException on the above program?


Founder of ExamLab and Systemup
See how I can help you to become an awesome programmer
Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

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

Joined: Sep 14, 2009
Posts: 1659
    
  11

try this:



that might work....


When you do things right, people won't be sure you've done anything at all.
Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

Janeice DelVecchio wrote:try this:



that might work....


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

Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1659
    
  11

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

Joined: Apr 25, 2008
Posts: 660

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

Joined: Sep 14, 2009
Posts: 1659
    
  11

I don't usually use console (I'm a scanner junkie)... so I am of no help to you
Amarnath Pandi
Greenhorn

Joined: Dec 04, 2009
Posts: 9
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

Joined: Apr 25, 2008
Posts: 660

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

Joined: Oct 13, 2005
Posts: 37958
    
  22
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

Joined: Apr 25, 2008
Posts: 660

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

Joined: Oct 13, 2005
Posts: 37958
    
  22
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

Joined: Oct 13, 2005
Posts: 37958
    
  22
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

Joined: Dec 04, 2009
Posts: 9
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

Joined: Oct 13, 2005
Posts: 37958
    
  22
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

Joined: Sep 29, 2008
Posts: 12617

I'm a little confused about setting the "password" variable to spaces--how does that make anything secure?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1659
    
  11

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

Joined: Oct 13, 2005
Posts: 37958
    
  22
What's unusual about Rob agreeing with you? Now, if Rob ever agrees with me . . .
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

I agree with people all the time, as long as they are right
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: [newbie] Null pointer exception ??
 
Similar Threads
please correct the code
sax parser with cdata
Why does cons remain equal to null?
Running some part of code as a Thread
Mail sending program