Wednesday, 31 August 2011

java.lang.NullPointerException

java.lang.NullPointerException


In java many times you face a "java.lang.NullPointerException" exception in running a program. The reason is variable at which the error occurred  does not contain any value or object.


1. If u define any reference variable and don't create any object for that variable 
2. If u define any reference variable  globally but when u create the object then again define it

ex.  JButton btn; /* defined globally */

     JButton btn=new JButton(); /* on the time of creating the object */

This  will give an "java.lang.NullPointerException" when u run the program or pass any event on this button.

solution :- 
1.  btn= new JButton  /* on the time of creating the object */
2. When u don't know then check that for your "object==null" if this is true then initialize object with some value

No comments:

Post a Comment