Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Null Pointer Exception

 
Vandiyadevan senthan
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Can anyone tell me ,when will the Null Pointer Exception throw?
Thanx in advance.
regards,
Vandiyadevan
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A NullPointerException is thrown when you try to reference a null Object variable (an object that is not there).
 
Kim Kantola
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 382
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic