嵌套对象空检查 [英] Nested objects null checking

查看:76
本文介绍了嵌套对象空检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道许多方法来检查嵌套对象中是否存在 NullPointerException ,因为对于Java 8,最简单的方法是应用 Optional< param> .或使用简单的if代码(例如

I know many ways how to check on nested objects for a NullPointerException, as for Java 8 the simplest is to apply the Optional<param>. or to use a simple if code such as

if(foo.bar() != null && foo.getBar().getObject()!=null){
    foo.getBar().getObject().getWhatever();
}

好吧,我的问题是,在不知道内部类/方法名称的情况下,是否有一种简便的方法可以做到这一点

well my question is is there an easy way to do so in one method without knowing the names of the inner classes/methods

更清楚地说,我有一个像这样的代码

to be more clear i have a code which is like this

contract.getContactInfo().getPosition()
contract.getEntitledPerson().getEmail()

我想做一个这样的例子

excel.setCell(name1,contract.getContactInfo().getPosition());
excel.setCell(name2,contract.getEntitledPerson().getEmail());

如果它仅适用于2个嵌套对象或仅2个setter,但对于50个excel单元格(有时带有5或6个嵌套对象)而言,这是一个真正的噩梦

if its for only 2 nested objects or only 2 setters that's fine but get it for a 50 excel cells with sometimes 5 or 6 nested objects its a real nightmare to do so

可以说像setCell方法

something lets say like the setCell method

public setCell(String name,Object object){
     return object; // make sure no nested object is null
}

我希望我的问题足够清楚.请记住,我不能简单地更改嵌套对象,因为其遗留代码已在很多地方使用!有什么想法吗?

i hope my question is clear enough. keep in mind i cant simply change the nested objects since its legacy code and being used in a lot of places! any ideas ?

推荐答案

正如您在问题中提到的那样,请使用 Optional ,但这更为宽松.

As you mentioned yourself in the question, use Optional, but it's more lenghty.

excel.setCell(name1, Optional.of(contract).map(Contract::getContactInfo).map(ContactInfo::getPosition).orElse(null));
excel.setCell(name2, Optional.of(contract).map(Contract::getEntitledPerson).map(Person::getEmail).orElse(null));

像这样格式化时更容易阅读:

Which is more easily read when formatted like this:

excel.setCell(name1, Optional.of(contract)
                             .map(Contract::getContactInfo)
                             .map(ContactInfo::getPosition)
                             .orElse(null));
excel.setCell(name2, Optional.of(contract)
                             .map(Contract::getEntitledPerson)
                             .map(Person::getEmail)
                             .orElse(null));

如果您的目标是最小的代码,则只需捕获 NullPointerException .在我看来,这有点骇人听闻,但是可以解决问题.

If your goal is smallest code, you could just catch the NullPointerException. It's a bit of a hack, in my opinion, but it'll do the trick.

首先,一个辅助方法:

public static <T> T nullGuard(Supplier<T> supplier) {
    try {
        return supplier.get();
    } catch (@SuppressWarnings("unused") NullPointerException ignored) {
        return null;
    }
}

然后包装有问题的表达式:

Then you wrapper the expression in question:

excel.setCell(name1, nullGuard(() -> contract.getContactInfo().getPosition()));
excel.setCell(name2, nullGuard(() -> contract.getEntitledPerson().getEmail()));

这篇关于嵌套对象空检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆