NullPointException
NullPointException is a RunTimeException. NullPointException is thrown when the value of a reference variable or object is null.
How to avoid NullPointException?
Before using any object or reference variable, you must check whether the object or reference variable is null or not and make sure you initialize it properly.
A simple NullPointException
public class NullPointExceptionExample {
public static void main(String args[]){
String name = null;
System.out.println(name.length());
// throw an NullPointException
}
}
If you run the program, nullpointexception will come as follows:
public class NullPointExceptionExample {
public static void main(String args[]){
String name = null;
// throw an NullPointException
try{
System.out.println(name.length());
}catch (NullPointerException e){
System.out.println("Object or
Refference Variable is Null");
}
}
}
Running this program will result in NullPointException but with a nice error message. So that the developers can easily understand where the mistake has been made.
Below are a few more examples of NullPointException
If you run the following program, NullPointException will come and program execution will stop.
Integer myInteger = null; // create a reference variable with null value
int n = myInteger.intValue (); // trying to get the value System.out.println (n); // throw an NullPointException
If you run the following program, NullPointException will come and a nice custom error message will appear.
String username = null;
try{
if(username.equals("Jone Doe")){
System.out.println("User Found!");
}else {
System.out.println("User not Found!");
}
}catch (NullPointerException e){
System.out.println("Username is null => " + e);
}
How to avoid NullPointException
#Pose1
To eliminate the NullPointException of this program, no error will occur if you write the program as below.
String username = null;
try{
if("Jone Doe".equals(username)){
System.out.println("User Found!");
}else {
System.out.println("User not Found!"); //executed block
}
}catch (NullPointerException e){
System.out.println("Username is null => " + e);
}
output:User not Found!
#Pose2
Let us create a function like the one below that will accept a string argument. If the sting is null, Exception will be thrown. Otherwise the length of the sting will print.
public static void main(String args[]){
String username = "";
try{
System.out.println(getLength(username));
}catch (IllegalArgumentException e){
System.out.println("String Can't be Empty
=> " + e);
}
}
The function is called in the middle of the main method
public static int getLength(String s){
if(s==null){
throw newIllegalArgumentException("String Cannot be null!");
}
return s.length();
}
output: 0
public static void main(String args[]){
String username = null;
try{
System.out.println(getLength(username));
}catch (IllegalArgumentException e){
System.out.println("String Can't be Empty => " + e);
}
}
output: String Can't be Empty =>java.lang.IllegalArgumentException: String Cannot be null
public static void main(String args[]){
String username = "Jone Doe";
try{
System.out.println(getLength(username));
}catch (IllegalArgumentException e){
System.out.println("String Can't be Empty
=> " + e);
}
}
output:8
Hope you have clear understand about NullPointException.