Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: avoid checking for null in nested classes (Deep Null checking)

Imagine I have a class Family. It contains a List of Person. Each (class) Person contains a (class) Address. Each (class) Address contains a (class) PostalCode. Any "intermediate" class can be null.

So, is there a simple way to get to PostalCode without having to check for null in every step? i.e., is there a way to avoid the following daisy chaining code? I know there's not "native" Java solution, but was hoping if anyone knows of a library or something. (checked Commons & Guava and didn't see anything)

if(family != null) {     if(family.getPeople() != null) {         if(family.people.get(0) != null) {             if(people.get(0).getAddress() != null) {                 if(people.get(0).getAddress().getPostalCode() != null) {                     //FINALLY MADE IT TO DO SOMETHING!!!                 }             }         }     } } 

No, can't change the structure. It's from a service I don't have control over.

No, I can't use Groovy and it's handy "Elvis" operator.

No, I'd prefer not to wait for Java 8 :D

I can't believe I'm the first dev ever to get sick 'n tired of writing code like this, but I haven't been able to find a solution.

like image 588
llappall Avatar asked Apr 30 '12 22:04

llappall


People also ask

How do I avoid multiple nulls in Java 8?

We can get rid of all those null checks by utilizing the Java 8 Optional type. The method map accepts a lambda expression of type Function and automatically wraps each function result into an Optional . That enables us to pipe multiple map operations in a row. Null checks are automatically handled under the hood.

What can help us in avoiding NullPointerException and null checks in Java?

Java 8 introduced an Optional class which is a nicer way to avoid NullPointerExceptions. You can use Optional to encapsulate the potential null values and pass or return it safely without worrying about the exception. Without Optional, when a method signature has return type of certain object.

Should you always check for null?

In any case, it is always good practice to CHECK for nulls on ANY parameters passed in before you attempt to operate on them, so you don't get NullPointerExceptions when someone passes you bad data. Show activity on this post. If you don't know whether you should do it, the chances are, you don't need to do it.


2 Answers

You can use for:

product.getLatestVersion().getProductData().getTradeItem().getInformationProviderOfTradeItem().getGln(); 

optional equivalent:

Optional.ofNullable(product).map(             Product::getLatestVersion         ).map(             ProductVersion::getProductData         ).map(             ProductData::getTradeItem         ).map(             TradeItemType::getInformationProviderOfTradeItem         ).map(             PartyInRoleType::getGln         ).orElse(null); 
like image 193
Robert Avatar answered Oct 01 '22 01:10

Robert


Your code behaves the same as

if(family != null &&   family.getPeople() != null &&   family.people.get(0) != null &&    family.people.get(0).getAddress() != null &&   family.people.get(0).getAddress().getPostalCode() != null) {         //My Code } 

Thanks to short circuiting evaluation, this is also safe, since the second condition will not be evaluated if the first is false, the 3rd won't be evaluated if the 2nd is false,.... and you will not get NPE because if it.

like image 33
amit Avatar answered Oct 01 '22 01:10

amit