Java:避免在嵌套类中检查null(深度空检查)

问题描述 投票:31回答:10

想象一下,我有一个班级家庭。它包含一个人员列表。每个(类)Person包含一个(类)地址。每个(类)地址包含一个(类)PostalCode。任何“中间”类都可以为null。

那么,有没有一种简单的方法来获取PostalCode,而不必在每一步检查null?即,有没有办法避免以下菊花链代码?我知道没有“原生”Java解决方案,但希望有人知道库或其他东西。 (已检查Commons&Guava并没有看到任何内容)

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!!!
                }
            }
        }
    }
}

不,不能改变结构。它来自我无法控制的服务。

不,我不能使用Groovy,它是方便的“猫王”操作员。

不,我宁愿不等待Java 8:D

我不敢相信我是第一个生病的人,厌倦了写这样的代码,但我找不到解决办法。

想法?

谢谢

- llappall

java class null ternary
10个回答
14
投票

您的代码行为与...相同

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
}

感谢short circuiting evaluation,这也是安全的,因为如果第一个是假的话,第二个条件将不会被评估,如果第二个是假的话,第三个条件将不会被评估,....并且你将不会得到NPE,因为如果它。


0
投票

和我最喜欢的,简单的try / catch,以避免嵌套的空检查......

try {
    if(order.getFulfillmentGroups().get(0).getAddress().getPostalCode() != null) {
        // your code
    } 
} catch(NullPointerException|IndexOutOfBoundsException e) {}

6
投票

您可以获得的最接近的是利用条件中的捷径规则:

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

}

顺便说一句,捕捉异常而不是提前测试条件是一个可怕的想法。


3
投票

如果很少见,你可以忽略null检查并依赖NullPointerException。 “罕见”由于可能的性能问题(取决于,通常会填写堆栈跟踪,这可能是昂贵的)。

除此之外1)一个特定的辅助方法,它检查null以清理该代码或2)使用反射和类似的字符串进行泛型方法:

checkNonNull(family, "people[0].address.postalcode")

实施留作练习。


2
投票

没有这么酷的想法,但如何捕获异常:

    try 
    {
        PostalCode pc = people.get(0).getAddress().getPostalCode();
    }
    catch(NullPointerException ex)
    {
        System.out.println("Gotcha");
    }

2
投票

你可以用于:

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

可选等价物:

Optional.ofNullable(product).map(
            Product::getLatestVersion
        ).map(
            ProductVersion::getProductData
        ).map(
            ProductData::getTradeItem
        ).map(
            TradeItemType::getInformationProviderOfTradeItem
        ).map(
            PartyInRoleType::getGln
        ).orElse(null);

1
投票

您可以使用某种版本的“null object”设计模式,而不是使用null。例如:

public class Family {
    private final PersonList people;
    public Family(PersonList people) {
        this.people = people;
    }

    public PersonList getPeople() {
        if (people == null) {
            return PersonList.NULL;
        }
        return people;
    }

    public boolean isNull() {
        return false;
    }

    public static Family NULL = new Family(PersonList.NULL) {
        @Override
        public boolean isNull() {
            return true;
        }
    };
}


import java.util.ArrayList;

public class PersonList extends ArrayList<Person> {
    @Override
    public Person get(int index) {
        Person person = null;
        try {
            person = super.get(index);
        } catch (ArrayIndexOutOfBoundsException e) {
            return Person.NULL;
        }
        if (person == null) {
            return Person.NULL;
        } else {
            return person;
        }
    }
    //... more List methods go here ...

    public boolean isNull() {
        return false;
    }

    public static PersonList NULL = new PersonList() {
        @Override
        public boolean isNull() {
            return true;
        }
    };
}

public class Person {
    private Address address;

    public Person(Address address) {
        this.address = address;
    }

    public Address getAddress() {
        if (address == null) {
            return Address.NULL;
        }
        return address;
    }
    public boolean isNull() {
        return false;
    }

    public static Person NULL = new Person(Address.NULL) {
        @Override
        public boolean isNull() {
            return true;
        }
    };
}

etc etc etc

然后你的if语句可以成为:

if (!family.getPeople().get(0).getAddress().getPostalCode.isNull()) {...}

这是次优的,因为:

  • 你被困在为每个班级制作NULL对象,
  • 很难使这些对象成为通用的,因此您不得不为要使用的每个List,Map等创建一个null对象版本,并且
  • 子类化可能存在一些有趣的问题,并且使用NULL。

但如果你真的讨厌你的== nulls,这是一个出路。


0
投票

我只是在寻找相同的东西(我的上下文:一堆自动创建的JAXB类,不知怎的,我有.getFoo().getBar()...的这些长菊花链。不可避免地,偶尔,中间的一个调用返回null,导致NPE 。

我开始摆弄一段时间的东西是基于反思。我相信我们可以使这个更漂亮和更高效(缓存反射,一方面,并​​定义“魔术”方法,如._all自动迭代集合的所有元素,如果中间的某些方法返回一个采集)。不漂亮,但也许有人可以告诉我们是否已经有更好的东西:

/**
 * Using {@link java.lang.reflect.Method}, apply the given methods (in daisy-chain fashion)
 * to the array of Objects x.
 * 
 * <p>For example, imagine that you'd like to express:
 * 
 * <pre><code>
 * Fubar[] out = new Fubar[x.length];
 * for (int i=0; {@code i<x.length}; i++) {
 *   out[i] = x[i].getFoo().getBar().getFubar();
 * }
 * </code></pre>
 * 
 * Unfortunately, the correct code that checks for nulls at every level of the
 * daisy-chain becomes a bit convoluted.
 * 
 * <p>So instead, this method does it all (checks included) in one call:
 * <pre><code>
 * Fubar[] out = apply(new Fubar[0], x, "getFoo", "getBar", "getFubar");
 * </code></pre>
 * 
 * <p>The cost, of course, is that it uses Reflection, which is slower than
 * direct calls to the methods.
 * @param type the type of the expected result
 * @param x the array of Objects
 * @param methods the methods to apply
 * @return
 */
@SuppressWarnings("unchecked")
public static <T> T[] apply(T[] type, Object[] x, String...methods) {
    int n = x.length;
    try {
        for (String methodName : methods) {
            Object[] out = new Object[n];
            for (int i=0; i<n; i++) {
                Object o = x[i];
                if (o != null) {
                    Method method = o.getClass().getMethod(methodName);
                    Object sub = method.invoke(o);
                    out[i] = sub;
                }
            }
            x = out;
        }
    T[] result = (T[])Array.newInstance(type.getClass().getComponentType(), n);
    for (int i=0; i<n; i++) {
            result[i] = (T)x[i];
    }
            return result;
    } catch (NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
            throw new RuntimeException(e);
    }
}

0
投票

如果,如果您使用的是java8,那么您可以使用;

resolve(() -> people.get(0).getAddress().getPostalCode());
    .ifPresent(System.out::println);

:
public static <T> Optional<T> resolve(Supplier<T> resolver) {
    try {
        T result = resolver.get();
        return Optional.ofNullable(result);
    }
    catch (NullPointerException e) {
        return Optional.empty();
    }
}

REF:avoid null checks


0
投票

虽然这篇文章差不多有五年之久了,但我可能还有另一种解决方法来解决如何处理NullPointerExceptions的古老问题。

简而言之:

end: {
   List<People> people = family.getPeople();            if(people == null || people.isEmpty()) break end;
   People person = people.get(0);                       if(person == null) break end;
   Address address = person.getAddress();               if(address == null) break end;
   PostalCode postalCode = address.getPostalCode();     if(postalCode == null) break end;

   System.out.println("Do stuff");
}

由于仍有大量遗留代码在使用,因此使用Java 8和Optional并不总是一种选择。

每当涉及深层嵌套类(JAXB,SOAP,JSON,你给它命名......)并且没有应用Law of Demeter时,你基本上必须检查所有内容并查看是否存在潜伏的NPE。

我提议的解决方案力求可读性,如果没有涉及至少3个或更多嵌套类,则不应该使用(当我说嵌套时,我不是指正式上下文中的Nested classes)。由于代码的读取比编写代码更多,因此快速浏览代码的左侧部分将使其含义比使用深层嵌套的if-else语句更清晰。

如果你需要else部分,你可以使用这个模式:

boolean prematureEnd = true;

end: {
   List<People> people = family.getPeople();            if(people == null || people.isEmpty()) break end;
   People person = people.get(0);                       if(person == null) break end;
   Address address = person.getAddress();               if(address == null) break end;
   PostalCode postalCode = address.getPostalCode();     if(postalCode == null) break end;

   System.out.println("Do stuff");
   prematureEnd = false;
}

if(prematureEnd) {
    System.out.println("The else part");
}

某些IDE会破坏这种格式,除非你指示它们不要(参见this question)。

您的条件必须反转 - 您应该告诉代码何时应该中断,而不是何时应该继续。

还有一件事 - 你的代码仍然容易破损。您必须使用if(family.getPeople() != null && !family.getPeople().isEmpty())作为代码中的第一行,否则空列表将抛出NPE。

© www.soinside.com 2019 - 2024. All rights reserved.