What happens if NullPointerException is not handled in main method of java?

Java 8Object Oriented ProgrammingProgramming

In Java there is default value for every type, when you don’t initialize the instance variables of a class Java compiler initializes them on your be-half with these values. Null is the default value of the object type, you can also manually assign null to objects in a method.

Object obj = null;

But, you cannot use an object with null value or (a null value instead of an object) if you do so, a NullPointerException will be thrown.

Following are some scenarios where NullPointerException occurs.

  • Calling the a method (instance) using null object.
public class Demo {
   public void demoMethod() {
      System.out.println("Hello how are you");
   }
   public static void main(String args[]) {
      Demo obj = null;
      obj.demoMethod();
   }
}

Run time exception

Exception in thread "main" java.lang.NullPointerException
at july_set3.Demo.main(Demo.java:11)
  • Accessing, modifying, printing, field of a null value(object).
public class Demo {
   String name = "Krishna";
   int age = 25;
   public static void main(String args[]) {
      Demo obj = null;
      System.out.println(obj.age);
      System.out.println(obj.name);
   }
}

Run time exception

Exception in thread "main" java.lang.NullPointerException
at july_set3.Demo.main(Demo.java:11)
  • Trying to access (print/use in statements) the length of null value.
public class Demo {
   String name = null;
   public static void main(String args[]) {
      Demo obj = new Demo();
      System.out.println(obj.name.length());
   }
}

Run time exception

Exception in thread "main" java.lang.NullPointerException
at july_set3.Demo.main(Demo.java:7)
  • Throw a null value.
public class Demo {
   public static void main(String args[]) {
      throw null;
   }
}

Runtime exception

Exception in thread "main" java.lang.NullPointerException
at july_set3.Demo.main(Demo.java:5)
  • Accessing or modifying elements/slots of a null value.
public class Demo {
   public static void main(String args[]) {
      int myArray[] = null;
      System.out.println(myArray[5]);
   }
}

Runtime exception

Exception in thread "main" java.lang.NullPointerException
   at july_set3.Demo.main(Demo.java:6)

Avoiding NullPointerException

To avoid NullPointerException

  • Make sure that all objects were initialized before using them.
  • Make sure that every reference variable (object, array etc.) is nut null, before accessing the fields and methods (if any) on it.

Handling NullPointerException

Yes, you can handle NullPointerException in the main method and display your own message. And, if you haven’t handled it the program terminates abnormally throwing NPE at the run time.

Example

public class Demo {
   public static void main(String args[]) {
      int myArray[] = null;
      try {
         System.out.println(myArray[5]);
      }catch(NullPointerException e){
         System.out.println("NPE occurred");
      }
   }
}

Output

NPE occurred

But, Since NullPointerException is a Runtime/unchecked exception there is no need to handle it at run time.

Moreover NPE occurs whenever there is a bug in the program which should be fixed, it is recommended to fix the bug or avoid it instead of trying to catch the exception.

raja
Published on 06-Aug-2019 11:20:55
Advertisements