java.lang.NullPointerException in Java

In Java “java.lang.NullPointerException” exception is the one that every beginner face once a while. The reason is variable at which the error occurred, does not contain any value or object reference.

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

Example:-

Object a; // declare an object

a.hashcode(); // trying to call a method on the object. 
//"mind it! I have not initialized it yet"

// above line will definitely give you an exception, 
//obviously NullPointerException as reference a is still null.

Solution:- 

Object a; // declare an object

a=new Object(); //initialized, Yes! you can do it on above line as will

a.hashcode();

 

Wanna deep dive? Go here