All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Avoid Null Pointer Exception In Java

Last Updated : Jul 1, 2023

How To Avoid Null Pointer Exception In Java

In this article we will show you the solution of how to avoid null pointer exception in java, when a Java program tries to use an object reference that contains a null value, the Null Pointer Exception kind of run-time exception is thrown.

The following situations can result in the null pointer exception being raised. We will now discuss the idea of how to avoid null pointer exceptions in java with an example.

Step By Step Guide On How To Avoid Null Pointer Exception In Java :-

Code 1

public class talkerscode {
static int value = 80;
public static int getValue(String a) {
if(s == null) {
throw new IllegalArgumentException("Arguments can not be null");
}
return value;
}
public static void main(String[] args) {
String a = null;
try {
System.out.println(getValue(s));
}catch (IllegalArgumentException ex) {
System.out.println("IllegalArgumentException caught");
}
a = "TC";
try {
System.out.println(getValue(s));
}catch (IllegalArgumentException ex) {
System.out.println("IllegalArgumentException caught");
}
}
}

Code 2

import java.io.*;
 class TalkersCode{
    public static void main (String[] args) {
        String myStr = "";
        try {
            System.out.println("String value:" + myStr);
            System.out.println("String Length:" + getLength(myStr));
        }
        catch(IllegalArgumentException e) {
            System.out.println("Exception: " + e.getMessage());
        }
        myStr = "Far from home";
        try {
            System.out.println("String value:" + myStr);
            System.out.println("String Length:" + getLength(myStr));
        }
        catch(IllegalArgumentException e) {
            System.out.println("Exception: " + e.getMessage());
        }
        myStr = null;
        try {
            System.out.println("String value:" + myStr);
            System.out.println("String Length:" + getLength(myStr));
        }
        catch(IllegalArgumentException e) {
            System.out.println("Exception: " + e.getMessage());
        }
    }
    public static int getLength(String myStr) {
        if (myStr == null) //throw Exception if String is null
            throw new IllegalArgumentException("The String argument cannot be null");
        return myStr.length();
    }
}
  1. "Talkerscode" is the name of the class.
  2. A static variable with the name "value" and the value 80 exists.
  3. A String parameter named "a" is required by the function "getValue".
  4. The "getValue" method's first line checks to see if the parameter "a" is null and raises an IllegalArgumentException with the message "Arguments can not be null" if it is.
  5. The method returns the value of the static variable "value" if the parameter is not null.
  6. The main technique begins.
  7. The initial value of the "a" String variable is null.
  8. The null String parameter "s" is used when calling the "getValue" function.
  9. The "IllegalArgumentException caught" message is printed by the try-catch block when it successfully catches the IllegalArgumentException that the "getValue" method throws.
  10. The letter "a" is given the value "TC."
  11. The null String parameter "s" is used to invoke the "getValue" function once more.
  12. The identical message "IllegalArgumentException caught" is printed after the try-catch block successfully catches the IllegalArgumentException that was thrown from the "getValue" method once more.
  13. The show is over.

Conclusion :-

As a result, we have successfully learned how to avoid null pointer exception in java with an example.

For Java developers, preventing and resolving null point exceptions is a crucial effort.

Java does not have any ways to check null pointer exceptions, in contrast to many other programming languages.

Developers therefore require extra tools to aid in the prevention of Java's NullPointerException.

I hope this article on how to avoid null pointer exception in java helps you and the steps and method mentioned above are easy to follow and implement.