NullPointerException explained
How many times has a NullPointerException in your code caused the code to break? In this blog post, I will be explaining what a NullPointerException is and what you can do to prevent it.
What is a NullPointerException?
A NullPointerException is a RuntimeException. It is an unchecked exception and so does not need to be handled by the code. It occurs when you try to manipulate an object that has a null value.
When does it occur ?
A NullPointerException can occur in several scenarios.
Scenario 1 – Invoking a method on an object that has a null value.
The most common scenarios where a NullPointerException occurs is when you invoke a method on an object that has a null value. Consider the following code snippet:
String str = "Hello World"; System.out.println("Length of the String is "+str.length()); str = null; System.out.println("Length of the String is "+str.length());
A variable called “str” is initialized to the value “Hello World“. Its length is then printed using the String.length method. Then the variable str is set to null . The str.length method is invoked again. When you run this code, it will print the following output:
Length of the String is 11 Exception in thread "main" java.lang.NullPointerException at learnjava.exceptions.NullPointerExceptionDemo.main(NullPointerExceptionDemo.java:9)
This error occurs since the str.length method is invoked after the variable str is initialized to null. So a method is invoked on a null object.
Scenario 2 – Accessing fields of an object that has a null value
A NullPointerException can also occur when you invoke the fields of an object with null value. Consider the following code snippet:
public class Rectangle { int length; int width; } public class RectangleDemo { public static void main(String[] args) { Rectangle r = null; int length = r.length; System.out.println("Length of the Rectangle is "+length); } }
In this code, there is a class called Rectangle with fields for length and width. There is another class called RectangleDemo. Here a Rectangle object called r is defined and initialized to null. Then, the length field of the Retangle r is accessed via the dot operator. Since the variable “r” has a null value, a NullPointerException will occur. When you run this code, it will print the following output:
Exception in thread "main" java.lang.NullPointerException at learnjava.exceptions.nullpointerdemo.RectangleDemo.main(RectangleDemo.java:7)
Scenario 3 – Accessing fields of an array with null value
A NullPointerException can also occur when you access the fields of an array that has a null value. Consider the following code snippet:
public static void main(String[] args) { int[] arr = null; System.out.println(arr[2]);
In the code above, an array called arr is defined. It is set to null. Then the 3rd element in the array is accessed using arr[2]. Since the array has a null value, this will cause a NullPointerException exception to occur. When you run this code, it will print the following output:
Exception in thread "main" java.lang.NullPointerException at learnjava.exceptions.nullpointerdemo.NullPointerArrayDemo.main(NullPointerArrayDemo.java:7)
How can you prevent it?
NullPointerException is an unchecked exception and so does not need to be handled by the code. You can prevent your code from breaking due to a NullPointerException by adding appropriate null checks.
So in Scenario 1 above, the error can be avoided using the following code snippet:
String str = "Hello World"; if(str != null) System.out.println("Length of the String is "+str.length()); str = null; if(str != null) System.out.println("Length of the String is "+str.length());
So now, a null check is added for the str object. If the str object has a null value, the str.length method will not be invoked. When this code is executed, it will print the following output:
Length of the String is 11