java.lang.NullPointerException
is the most commonly encountered exceptions while execution of applications written in java.
All java programmers are haunted by this exception since it mostly arises due to incorrect handling of null
references.
This post will cover topics such as what it is, what is its cause and what is the solution to prevent it.
As its name states, a
java.lang.NullPointerException
is raised when the application code tries to call a method on a null reference.A
null
reference means an object variable which has not been initialized or which does not point to any memory location.Example,
String emptyRef; String lowerCase = emptyRef.toString();
In the above example, emptyRef
is a string variable declared but not initialized which means it is null
.
Calling toString
method on this reference translates to null.toString()
and will raise a NullPointerException
.
NullPointerException
is an unchecked exception.Javadoc for NullPointerException
states
Thrown when an application attempts to use null in a case where an object is required.
Causes
A NullPointerException
hereby referred to a NPE is not only raised when calling a method on null reference but it may also arise when accessing a field of a null object.
Below are the situations where a NPE can occur.
1. Calling a method on null
reference.
2. Accessing field(instance variable) of a null
reference.
3. Trying to get the length of an array when it is null. Example, Integer[] arr = null; int l = arr.length;
4. Accessing array location when the array is null. Example, Integer[] arr = null; arr[1] = 10;
There are a couple of ways in which a NPE can be prevented. They are detailed below.
1. When creating an object, initialize it always.
In other words, give a default value other than
null
to an object.For String objects, initialize them with empty value, for arrays, initialize array elements with either 0 for integer arrays, empty string for String array etc., and for other types create empty objects.
Example,
String str1 = ""; // empty string literal String str2 = new String(""); // empty string object Integer value = 0; // integer object initialized to 0 MyClass obj = new MyClass(); // custom object initialized to empty
In all the above cases, no object is initialized to null
.
2. When calling a method, accessing a field on an object always perform a null
check. Example,
int getLength(String s) { if(s != null) { return s.length(); } return 0; }
Many times, you call a method on a object which is supplied/created by an external library or class.
It might happen that the external library/class was not able to create the object properly due to some error or missing values and returned a null object.
Before calling any method on this object, a null
check should be performed.
Example,
void readInput() { Scanner scanner = new Scanner(System.in); // check for null before calling method if(scanner != null) { scanner.readLine(); } }
Catching NullPointerException
If even after above prevention steps, you still want that NPE should not be seen at all, then you can also catch this exception using a
try
–catch
block.First identify the place where a NPE can arise, surround that code with
try
–catch
block as shown below.void readInput() { try { Scanner scanner = new Scanner(System.in); // check for null before calling method scanner.readLine(); } catch(NullPointerException e) { e.printStackTrace(); } }
That’s it on NPE. For any other clarifications/queries/feedback, refer to comment section below.
0