Null Pointer Exception in Java Programming



NullPointerException is a runtime exception and it is thrown when the application try to use an object reference which has a null value.

For example, using a method on a null reference.

Example

Live Demo

public class Tester {
   public static void main(String[] args) {
      Object ref = null;
      ref.toString(); // this will throw a NullPointerException
   }
}

Output

Exception in thread "main" java.lang.NullPointerException
	at Tester.main(Tester.java:4)

Advertisements