In order to avoid NullPointerException, we must make sure that all objects are correctly initialised before the variable is used. Here we will see How to handle Null Pointer Exception in Java, Best way to avoid Null Pointer Exception in Java. Identifying the NullPointerException is quite simple. The NullPointerException print stack trace will display the exception class name and line number. Then look at the code to see what could be null that causes the NullPointerException. Exception in thread 'main' Īt (TestNullPointerException.java:8) # tinyumbrella code# The occurs in the program when you perform a java variable operation that is assigned to null. There are some common reasons to throw in Java program.Ĭalling the instance method of a null object.Īccessing or modifying the field of a null object. Taking the length of null as if it were an array.Īccessing or modifying the slots of null as if it were an array.