HomeLinuxLearn how to Resolve the java.lang.NullPointerException

Learn how to Resolve the java.lang.NullPointerException


In programming languages, encountering errors enable us to execute varied functionalities appropriately by prompting the developer. In such a case, there will be cases the place the developer can face the “java.lang.NullPointerException” upon a specific “null” worth whereas coping with bulk knowledge. This exception must be dealt with with a view to streamline the code functionalities.

This weblog will elaborate on the approaches to deal with the “java.lang.NullPointerException”.

Learn how to Resolve the “java.lang.NullPointerException”?

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

Demonstration of “java.lang.NullPointerException”

Within the below-given illustration, we are going to see how the mentioned exception happens:

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

 
Within the above code snippet:

    • Firstly, initialize a string with a “null” worth.
    • Within the subsequent step, invoke the perform customFunc() by passing the declared string as its argument.
    • After that, outline a perform named “customFunc()” by specifying a string that must be handed as its parameter.
    • Within the perform definition, return the size of the handed string by way of the “size” property.
    • This code will perform such that by passing a “null” worth to the perform, the mentioned exception will probably be thrown:


Within the above output, it may be noticed that the “NullPointerException” is thrown upon invoking “null” as a perform argument.

Method 1: Resolve the “java.lang.NullPointerException” Utilizing “attempt…catch” Statements

The “attempt” assertion assists in defining a code block that must be examined for errors whereas being executed and the “catch” assertion comes into impact in case of an encountered limitation within the attempt block. Extra particularly, the “java.lang.NullPointerException” will be catered by making use of the specified operation within the “attempt” block and inserting the possible exception within the “catch” block.

Syntax

attempt {
 The code block to attempt
}
catch(Exception e) {
 The code block to deal with the confronted exception in the attempt block
}

 
Instance

Let’s overview the below-stated instance:

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

 
Within the above strains of code:

    • Recall the mentioned approaches to initialize a “null” worth and invoke the acknowledged perform by passing the null worth as its argument.
    • Now, likewise, outline a perform named “customFunc()” having the string to be handed as its parameter.
    • After that, apply the “attempt” assertion to fetch the index of the desired character in a string by way of the “indexOf()” technique.
    • Observe that the “attempt” assertion will be executed if the string isn’t null. For the reason that worth is “null”, so an exception will probably be encountered which will probably be dealt with by the “catch” assertion.

Output


Within the above output, it may be seen that the desired exception is catered by the “catch” assertion and so it’s thrown, thereby resolving it.

Method 2: Resolve the “java.lang.NullPointerException” Utilizing “if/else” Assertion

The mentioned exception will also be sorted out by way of the “if/else” assertion. This may be finished by merely performing the specified operation within the “if” assertion and upon the unhappy situation, an exception is returned by way of the “else” assertion.

Syntax

if (cond) {
 The assertion to be executed upon the glad situation i.e “cond”
}
else {
 The assertion to be executed upon the unhappy “if” situation.
}

 
Instance

Undergo the next instance to have an understanding of dealing 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 within the above code:

    • Repeat the mentioned steps for initializing a null worth and invoking the perform by passing the “null” worth as its argument.
    • Subsequent, equally declare the perform “customFunc()” having the desired parameter.
    • Now, apply the “if/else” assertion such that upon the glad situation within the “if” assertion, the corresponding block executes.
    • Within the different case, the “else” block will come into impact and throw the mentioned exception.
    • Observe that the situation within the “if” assertion is similar to the previous method, thereby falsifying it.

Output


That’s how one can deal with the “java.lang.NullPointerException”.

Conclusion

The “java.lang.NullPointerException” is returned when a reference variable is invoked or de-referenced and isn’t referring to any object. This exception will be dealt with by inserting it within the “attempt…catch” assertion such that upon executing the “attempt” block, the confronted exception will be catered by the latter block. Additionally, the “if/else” assertion will be utilized to resolve this exception by inserting it within the “else” assertion if the “if” assertion fails to execute. This weblog mentioned the approaches to deal with the “java.lang.NullPointerException”.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments