vendredi 7 septembre 2018

NULL safe object checking in JAVA 8

So i want to do a null safe check on a value contained within a value.

So I have 3 objects contained within each other:

Person has a clothes object which has a country object which has a capital

So a person may not have clothes so a check like this throws a null pointer:

if (person.getClothes.getCountry.getCapital)

How would I make a statement like this just return false if any of the objects on the path are null?

I also don't want to do it this way. (A one-liner in Java-8 if possible.

if (person !=null) {
    if (person.getClothes != null) {
        if (person.getClothes.getCountry !=null) {
            etc....
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire