Tuesday, March 1, 2022

Null Pointer Exception in Java

Here we will discuss about what is nullpointerexception and how to fix it?

In java if a variable or object reference do not have any value to assign to them , then we will have possibility of having null pointer exception. More clearly if a reference variable or object is assigned to nothing. For example,

public class NullPointerException {
public static void main(String[] args) {
Employee employee = null;
employee.getName();
}
}

Lets run this program, we are getting exception says below.,

Exception in thread "main" java.lang.NullPointerException
at com.example.monitoring.exception.NullPointerException.main(NullPointerException.java:8)
Here we are trying to call getName() of employee object, but we did not create a object for it.  Which mean employee is referred to nothing . 

How to fix this issue, we need to create a new employee object using new Employee().After changing null to this we will not get exception. We are not assigned any value to employee name variable, even we don't get any exception, because we have not call any further from getName(). Let me modified something from above example,

public class NullPointerException {
public static void main(String[] args) {
Employee employee = new Employee();
employee.getName().length();
}
}

While running the main method we will get below exception
Exception in thread "main" java.lang.NullPointerException
at com.example.monitoring.exception.NullPointerException.main(NullPointerException.java:8).

How to fix this issue, before we fix it, let we check other possible ways of having null pointer exception.

public class NullPointerException {
public static void main(String[] args) {
List<Employee> employeeList = null;
for(Employee employee1 : employeeList){
System.out.println(employee1);
}
}
}
Here we will get nullpointerexception on for loop, because we are trying to iterate from nothing.

List<Employee> employeeList = null;
Employee employee = new Employee();
employeeList.add(employee);
for(Employee employee1 : employeeList){
if(employee1.getName().equals("Boomiraj")){
System.out.println("Hello Boomiraj");
}
}

We will get exception on if condition to check equals(), because we do not set name to the employee object, so employee1 will have null on name variable. 

public static void main(String[] args) {
Employee employee = new Employee();
getEmployeeNameLength(employee);
}

public static int getEmployeeNameLength(Employee employee){
return employee.getName().length();
}

Here we will get exception on return statement of getEmployeeNameLength , here the exception trace will be like this and more details on it, 


Exception in thread "main" java.lang.NullPointerException
at com.example.monitoring.exception.NullPointerException.getEmployeeNameLength(NullPointerException.java:15)
at com.example.monitoring.exception.NullPointerException.main(NullPointerException.java:11)

We have seen all the possible ways of having nullpointerexception. Let see how to fix them.

Never assign object reference to null, just initialize them new Employee();. We may have some scenarios to have dynamic object, there we cannot initialize object. we can handle them with condition check using null. let see above code we are trying to get length of the name from employee object. 

We need to check the getName is null or not before we call .length(). Before call getName() we need to check employee object itself null or not.

public static int getEmployeeNameLength(Employee employee){
if(employee != null &&
employee.getName() != null)
{
return employee.getName().length();
}
return 0;
}
Above code might be looks like ugly code, we don't have any other option to check it.

On employee name equals check we need to pass static value first then we need to check equality.

if("Boomiraj".equals(employee.getName())){
System.out.println("Hello Boomiraj");
}

For loop fix we need initialize the list object.
List<Employee> employeeList = new ArrayList<>();

Now we have fixed all the possible ways of null exception. 

But this could not be happen to primitive type. Because we cannot declare a primitive type as null, primitive types always have their default value. Lets try with example.

Boolean isNull = null;
if(isNull){
System.out.println("Its null!");
}
 Lets try with example. Here I used Boolean wrapper class. So we can assign null to it, but if we use primitive boolean we cannot assign null to it. 

boolean isNull = null; 
Here I am getting compilation error says, required type boolean, provider null.
If you have possible way to declare primitive type, use them to rid out of nullpointerexception.

Thank you!.

No comments:

Post a Comment