Java

How to Resolve the java.lang.NullPointerException

In programming languages, encountering errors allow us to execute various functionalities appropriately by prompting the developer. In such a case, there can be instances where the developer can face the “java.lang.NullPointerException” upon a particular “null” value while dealing with bulk data. This exception needs to be handled in order to streamline the code functionalities.

This blog will elaborate on the approaches to cope with the “java.lang.NullPointerException”.

How to Resolve the “java.lang.NullPointerException”?

The “java.lang.NullPointerException” is returned when a reference variable is invoked (or de-referenced) and is not referring to any object.

Demonstration of “java.lang.NullPointerException”

In the below-given illustration, we will see how the discussed exception occurs:

String s = null;
customFunc(s);
static void customFunc(String str) {
System.out.println(str.length());
}

 
In the above code snippet:

    • Firstly, initialize a string with a “null” value.
    • In the next step, invoke the function customFunc() by passing the declared string as its argument.
    • After that, define a function named “customFunc()” by specifying a string that needs to be passed as its parameter.
    • In the function definition, return the length of the passed string via the “length” property.
    • This code will function such that by passing a “null” value to the function, the discussed exception will be thrown:


In the above output, it can be observed that the “NullPointerException” is thrown upon invoking “null” as a function argument.

Approach 1: Resolve the “java.lang.NullPointerException” Using “try…catch” Statements

The “try” statement assists in defining a code block that needs to be tested for errors while being executed and the “catch” statement comes into effect in case of an encountered limitation in the try block. More specifically, the “java.lang.NullPointerException” can be catered by applying the desired operation in the “try” block and placing the probable exception in the “catch” block.

Syntax

try {
 The code block to try
}
catch(Exception e) {
 The code block to handle the faced exception in the try block
}

 
Example

Let’s overview the below-stated example:

String s = null;
customFunc(s);
static void customFunc(String str) {
try {
System.out.println("First character: " + str.indexOf(0));
}
catch(NullPointerException e) {
System.out.println("NullPointerException thrown!");
}}

 
In the above lines of code:

    • Recall the discussed approaches to initialize a “null” value and invoke the stated function by passing the null value as its argument.
    • Now, likewise, define a function named “customFunc()” having the string to be passed as its parameter.
    • After that, apply the “try” statement to fetch the index of the specified character in a string via the “indexOf()” method.
    • Note that the “try” statement can be executed if the string is not null. Since the value is “null”, so an exception will be encountered which will be handled by the “catch” statement.

Output


In the above output, it can be seen that the specified exception is catered by the “catch” statement and so it is thrown, thereby resolving it.

Approach 2: Resolve the “java.lang.NullPointerException” Using “if/else” Statement

The discussed exception can also be sorted out via the “if/else” statement. This can be done by simply performing the desired operation in the “if” statement and upon the unsatisfied condition, an exception is returned via the “else” statement.

Syntax

if (cond) {
 The statement to be executed upon the satisfied condition i.e “cond”
}
else {
 The statement to be executed upon the unsatisfied "if" condition.
}

 
Example

Go through the following example to have an understanding of coping with the exception:

String s = null;
customFunc(s);
static void customFunc(String str) {
if(str != null) {
System.out.println("First character: " + str.indexOf(0));
}
else {
System.out.println("NullPointerException thrown!");
}
}

 
Apply the below-discussed steps as given in the above code:

    • Repeat the discussed steps for initializing a null value and invoking the function by passing the “null” value as its argument.
    • Next, similarly declare the function “customFunc()” having the specified parameter.
    • Now, apply the “if/else” statement such that upon the satisfied condition in the “if” statement, the corresponding block executes.
    • In the other case, the “else” block will come into effect and throw the discussed exception.
    • Note that the condition in the “if” statement is identical to the former approach, thereby falsifying it.

Output


That’s how you can handle the “java.lang.NullPointerException”.

Conclusion

The “java.lang.NullPointerException” is returned when a reference variable is invoked or de-referenced and is not referring to any object. This exception can be handled by placing it in the “try…catch” statement such that upon executing the “try” block, the faced exception can be catered by the latter block. Also, the “if/else” statement can be utilized to resolve this exception by placing it in the “else” statement if the “if” statement fails to execute. This blog discussed the approaches to cope with the “java.lang.NullPointerException”.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.