Thursday, 12 March 2015

Java: handling null in getter method chains

Null is just something you need to deal with in Java. This post looks at the mechanism for handling chained method calls without causing a NullPointerException.

This post uses Java 8.


Here's a deep object graph for illustration purposes:

public class Root {
    private Trunk trunk;

    public Trunk getTrunk() { return trunk; }
    public void setTrunk(Trunk trunk) { this.trunk = trunk; }

    public static class Trunk {
        private Branch branch;

        public Branch getBranch() { return branch; }
        public void setBranch(Branch branch) { this.branch = branch; }

    public static class Branch {
        private Leaf leaf;

        public Leaf getLeaf() { return leaf; }
        public void setLeaf(Leaf leaf) { this.leaf = leaf; }

    public static class Leaf {
        private String name;

        public String getName() { return name; }
        public void setName(String name) { = name; }

Given a Root reference variable there are a lot of null checks between it and

    public static void printLeafName(Root root) {
        String name = null;
        if(root != null) {
            Root.Trunk trunk = root.getTrunk();
            if (trunk != null) {
                Root.Branch branch = trunk.getBranch();
                if (branch != null) {
                    Root.Leaf leaf = branch.getLeaf();
                    if (leaf != null) {
                        name = leaf.getName();

Some languages provide facilities for handling this, like Groovy's safe navigation operator:

def void printLeafName(Root root) {
    def name = root?.trunk?.branch?.leaf?.name

Java lacks such an operator.

Ternary operator

Java's conditional operator often provides a succinct alternative to if statements but not in this case:

    public static void printLeafName(Root root) {
        String name = ((root == null)
                || (root.getTrunk() == null)
                || (root.getTrunk().getBranch() == null)
                || (root.getTrunk().getBranch().getLeaf() == null))
                ? null
                : root.getTrunk().getBranch().getLeaf().getName();

It also comes at the expense of more getter calls, cheap though they are.


Combining the Java 8 Optional type with method references gives reasonably clear code:

    public static void printLeafName(Root root) {
        String name = Optional.ofNullable(root)

The downside here is object allocation.


The KλudJe library takes a tangential approach to using method references to achieve the same thing with the Nullifier type:

    public static void printLeafName(Root root) {
        String name = Nullifier.eval(root, Root::getTrunk, Root.Trunk::getBranch, Root.Branch::getLeaf, Root.Leaf::getName);

The advantage of this example over Optional is that there are no object allocations because Oracle JDK8 will reuse static method references.

No comments:

Post a Comment

All comments are moderated