Home » How to avoid null pointer exception in Java

How to avoid null pointer exception in Java

by Online Tutorials Library

How to avoid null pointer exception in Java?

Null Pointer Exception is a kind of run time exception that is thrown when the java program attempts to use the object reference that that contains the null value. The null pointer exception can be thrown in the following scenarios.

1. The method is invoked using a null object

Java program throws a NullPointerException if we invoke some method on the null object. Consider the following example.

Output:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "DummyClass.convert(String)" because "dummy" is null  at DummyClass.main(DummyClass.java:14)  

2. The program tries to modify the null object’s field.

The null pointer exception can be thrown if we try to modify the field for a null object. Consider the following example.

Output:

Exception in thread "main" java.lang.NullPointerException: Cannot read field "num" because "dummy" is null  at DummyClass.main(DummyClass.java:10)  

3. Passing a null argument to the method

If we do not check the arguments of a method for the null values, the null pointer exception can occur. Consider the following example.

Output:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.toUpperCase()" because "s" is null  at DummyClass.convert(DummyClass.java:4)  at DummyClass.main(DummyClass.java:9)  

4. Tries to operate on the array object which is null

However, if we try to perform an operation on a null array, the null pointer exception will be thrown. Consider the following example.

Output:

Exception in thread "main" java.lang.NullPointerException: Cannot read the array length because "dummy.arr" is null  at DummyClass.main(DummyClass.java:7)  

5. Tries to synchronize over a null object.

When we try to synchronize the method or block for the concurrent access, we must check whether the object reference we use for the synchronization is null. Consider the following example.

Output:

Exception in thread "main" java.lang.NullPointerException: Cannot enter synchronized block because "DummyClass.var" is null  at DummyClass.main(DummyClass.java:6)  

Avoiding null pointer exception has been an important task for Java developers. Many programming languages provide methods to check null pointer exceptions. However, Java doesn’t provide such methods.

To avoid Null pointer exceptions, we need to ensure that all objects are initialized with a legitimate value before using them. We must verify at the time of defining a reference variable that it is not null since performing any operations on a null reference variable leads to the null pointer exception.

There are the following scenarios that we need to consider while dealing with the null pointer exceptions.

String Comparisons

When we compare the strings to literals, there might be the case that we encounter a null pointer exception if the string object that we are comparing is null. Consider the following example.

Output:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.equals(Object)" because "str" is null  at StringCompare.main(StringCompare.java:7)  

The above example will give null pointer exceptions since the string object which calls the equals () method is null. However, we can avoid such scenarios if we call the equals () method on a String literal (whose value is already known) rather than a String object. Consider the following example, which doesn’t give the null pointer exception.

Output:

Different Objects  

Using Ternary operator

We can also use the ternary operator to avoid the null pointer exceptions. We can put the check using the ternary operator. We check the Boolean expression and return value 1 if the expression is true; otherwise, return value 2.

Consider the following example.

Output:

null value TutorAspire JTP  

In the above example, if the string object is null, the msg will be a null value; otherwise, it will print the actual string.

Checking arguments of the method

We can check on the arguments of a method for null values before executing the body of the method. Consider the following example, which throws an IllegalArgumentException when the arguments passed in the method are not legitimate.

Output:

IllegalArgumentException caught  50  

You may also like