Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes Null Pointer Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Null Pointer Exception" Watch "Null Pointer Exception" New topic
Author

Null Pointer Exception

Vandiyadevan senthan
Ranch Hand

Joined: Nov 11, 2003
Posts: 37
Hi,
Can anyone tell me ,when will the Null Pointer Exception throw?
Thanx in advance.
regards,
Vandiyadevan
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
A NullPointerException is thrown when you try to reference a null Object variable (an object that is not there).


- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. - What truth? - That there is no spoon!!!
Kim Kantola
Ranch Hand

Joined: May 17, 2001
Posts: 274
Another example would be when you try to call a method of an object when the object is null

The call of the intValue() method of the Integer object x will throw a Null Pointer exception since x is null. If you are getting an object from another method and are not sure if it would be null or not, this is a good idea so that you won't hit the exception :
Sadanand Murthy
Ranch Hand

Joined: Nov 26, 2003
Posts: 382
Originally posted by Vicken Karaoghlanian:
A NullPointerException is thrown when you try to reference a null Object variable (an object that is not there).

Vandiyadevan senthan ,
Keep in mind that even if an array obj is initialized, you may get NPE.
Example:
(where i >=0 and i <5) will give NPE since SomeClass[ i ] has not been initialized & is null.


Ever Existing, Ever Conscious, Ever-new Bliss
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Null Pointer Exception