Null Pointer Exception in Java Programming

Java 8Object Oriented ProgrammingProgramming

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)
raja
Published on 03-Dec-2018 15:42:30
Advertisements