No Need To Check Nulls?

Deepak Singh
3 min readApr 22, 2020

java.lang.NullPointerException

This is the common exception which every developer deals with, each day. Today we are going to mitigate your chances to meet this exception and believe me if you use the below strategy you are going to miss this exception.

Today we’ll be discussing Null Pointer Design Patten.

Null Object Pattern

— motivation: references may be null !!!
— it can be very elaborate to deal with null references
— we have to check whether a reference is null or not ->

NullPointerException

if we call methods on null objects

GOOD TO AVOID NULL REFERENCES For example: return empty ArrayList instead of null …
Or we have to use several if() checks whether the reference is null
For example: dealing with binary trees

class Node {
private int data;
private Node leftChild;
private Node rightChild;

}

SOLUTION:

The null object design pattern
We create an abstract class specifying various operations to be done Concrete classes extending this class and a null object class providing do-nothing implementation of this class
~ this approach can be used where we need to check the null value !!!

Let’s discuss this with some real example:

We’ll be taking an example of for customers from Database:

let’s assume we have a customer, abstract class:

public abstract class AbstractCustomer {
protected String personName;
public abstract boolean isNull();
public abstract String getPerson();
}

And a real customer definition like this:

public class RealCustomer extends AbstractCustomer {public RealCustomer(String personName){
this.personName = personName;
}

@Override
public boolean isNull() {
return false;
}
@Override
public String getPerson() {
return this.personName;
}
}

And a null customer definition like this:

public class NullCustomer extends AbstractCustomer {

@Override
public boolean isNull() {
return true;
}
@Override
public String getPerson() {
return "No person with the given name in the database...";
// this is hou we are handeling a null person so if a null customer //is called we'll be getting a valid error.
}
}

Now let’s create a dummy db:

public class Database {private List<String> customerNames;

public Database(){
this.customerNames = new ArrayList<>();
this.customerNames.add("Deepak");
this.customerNames.add("DJ");
this.customerNames.add("Ronak");
this.customerNames.add("Piyush");
}
public boolean existCustomer(String name) {

for(String customer : this.customerNames){
if( customer.equals(name)){
return true;
}
}

return false;
}
}

We have a customer factory like this:

// this class will be used to get customers from database assume it as a dao.
public class CustomerFactory {
private Database database;

public CustomerFactory(){
this.database = new Database();
}

public AbstractCustomer getCustomer(String name){

if( checkName(name) ){
return new RealCustomer(name);
}

return new NullCustomer();
}
private boolean checkName(String name) {
if( database.existCustomer(name) ){
return true;
}

return false;
}
}

Now let’s try to get some customers:

public static void main(String[] args) {

CustomerFactory customerFactory = new CustomerFactory();

System.out.println( customerFactory.getCustomer("Ronak").getPerson() );//real customer
System.out.println( customerFactory.getCustomer("Deep").getPerson() );//Null customer
System.out.println( customerFactory.getCustomer("Dhananjay").getPerson() );//Null Customer
System.out.println( customerFactory.getCustomer("Piyush").getPerson() );//real customer.
}

So now we are trying to get two customers who don’t even exist in DB. but we will not be getting any exception here. we’ll be getting a valid error message.

This is how you can prevent Null pointer exceptions and give the users a clear human-friendly message when they try to run some method on Null Objects.

Stay tuned the next is the Factory pattern which I often use.

--

--