Avoiding NullPointerException in Java

Ramit Raj
2 min readAug 21, 2023

--

Whenever we write Java codes we should always take care of the NullPointer exception, before moving further lets talk about what is NullPointer exception.

Suppose we have a class Student

@Data
public class Student{
private Integer rollNumber;
private String name;
private String address;
}

Now we want to fetch some data from Student class like its rollNumber or name but what if student objects itself is null and we are fetching a value from a null value instead of a valid student object.

Student student=fetchStudentDetails(rollNumber);  //will return student or null.
String studentName =student.getName(); // here the issue will occur

So there is a method named fetchStudentDetails(rollNumber) which will return us Student object and if not found will return null in the next line we are seeing from the student object which in our case considering as null and from that we are fetching name which will eventually break the code and throw nullPointer exception.

Lets resolve it so that even if this student.getName() is called it will not throw any exception rather it will store null in studentName which is rather a good thing as compared to exception.

public class Util{
public static <T> Optional<T> resolveNull(Supplier<T> supplier){
try{
T value= supplier.get();
return Optional.ofNullable(value);
}
catch(NullPointerException exception){
// we log the exception
}
return Optional.empty();
}
public static <T> T fetchValue(Supplier<T> supplier){
return resolveNull(()->supplier.get()).orElse(null);
}
}

we will create a separate class name Util where we will create 2 methods as shown above the main method which we will be using fetchValue(Supplier<T> supplier) to avoid the NullPointerException.

Lets see how we can use it and its vast utilization.

@Data
public class Student{
private Integer rollNumber;
private String name;
private String address;
}
-----------------------------------------
@Data
public class College{
private Integer collegeId;
private List<Student> student;
private String collegeName;
}
------------------------------------------
@Data
public class University{
private Integer enrollmentNumber;
private List<College> college;
private String address;
}

In the above code we can see how Student, College, University are related to each other lets write a code and see how we can we utilize our fetchValue() method written above.


public String findStudent(University university){
String studentName= university.getCollege().get(0).getStudent().get(0).getName();
// if anytime one value becomes null the it will break the code and NullPointer
// exception will arise.

String studentName= fetchValue(()->university.getCollege().get(0).getStudent().get(0).getName());
// anytime a value becomes null student name will be null and no exception thrown.
}

From the above code we can see that the second line where we have used fetchValue() method we can avoid the NullPointerException and its very useful in scenarios where we have chain events some of its usage will be in condition checks like if conditions to check whether the value is present or not .

if(Objects.nonNull(fetchValue(()->university.getCollege().get(0).getStudent().get(0).getName()))){
// if the value present it will come inside else it will return null to
// Objects.nonNull which will make it false and not come inside this conditon.
}

Happy Learning. Thank You!

--

--

Ramit Raj

Java Springboot Developer with 3+ yrs of experience. Skilled in developing robust, scalable applications. Passionate about coding and constantly learning.