How To Avoid Null Pointer Exception In Java
Last Updated : Mar 11, 2024
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(); } }
- "Talkerscode" is the name of the class.
- A static variable with the name "value" and the value 80 exists.
- A String parameter named "a" is required by the function "getValue".
- 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.
- The method returns the value of the static variable "value" if the parameter is not null.
- The main technique begins.
- The initial value of the "a" String variable is null.
- The null String parameter "s" is used when calling the "getValue" function.
- The "IllegalArgumentException caught" message is printed by the try-catch block when it successfully catches the IllegalArgumentException that the "getValue" method throws.
- The letter "a" is given the value "TC."
- The null String parameter "s" is used to invoke the "getValue" function once more.
- 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.
- 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.