如何在运行UBL XML读取时使用很少的ifs检查null

How can I check for null using few ifs while running UBL XML Reading

提问人:neylersin 提问时间:7/16/2023 更新时间:7/16/2023 访问量:42

问:

正如您在下面的代码中看到的示例,我需要使用大量 if。否则,它将引发空指针异常。

if (despt.getDespatchSupplierParty().getParty().getContact() != null) {
                                    if (despt.getDespatchSupplierParty().getParty().getContact()
                                            .getTelephone() != null) {
                                        tfdesp000_arch.setDcpPartyContactTelephone(despt.getDespatchSupplierParty()
                                                .getParty().getContact().getTelephone().getValue());
                                    }

                                    if (despt.getDespatchSupplierParty().getParty().getContact().getTelefax() != null) {
                                        tfdesp000_arch.setDcpPartyContactTelefax(despt.getDespatchSupplierParty()
                                                .getParty().getContact().getTelefax().getValue());
                                    }

                                    if (despt.getDespatchSupplierParty().getParty().getContact()
                                            .getElectronicMail() != null) {
                                        tfdesp000_arch.setDspPartyContactElectronicMail(despt.getDespatchSupplierParty()
                                                .getParty().getContact().getElectronicMail().getValue());
                                    }

                                    if (despt.getDespatchSupplierParty().getParty().getContact().getName() != null) {
                                        tfdesp000_arch.setDspContactName(despt.getDespatchSupplierParty().getParty()
                                                .getContact().getName().getValue());
                                    }
                                }

如何使用很少的 if 来避免此空指针异常。

Java XML nullPointerException ubl

评论


答:

0赞 tgdavies 7/16/2023 #1

您可以编写一个函数来执行 null 检查并重用它:

import java.util.function.Consumer;
import java.util.function.Function;

interface Despatch {

    SupplierParty getDespatchSupplierParty();
}
interface SupplierParty {

    Party getParty();
}
interface Party {
    Contact getContact();
}
interface Contact {
    Telephone getTelephone();

    Telefax getTelefax();

    EMail getElectronicMail();

    Name getName();
}
interface Telephone {
    String getValue();
}
interface Telefax {

    String getValue();
}
interface EMail {
    String getValue();
}
interface Name {
    String getValue();

}
interface TFDESP000_ARCH {

    void setDcpPartyContactTelephone(String value);

    void setDcpPartyContactTelefax(String value);

    void setDspPartyContactElectronicMail(String value);

    void setDspContactName(String value);
}
public class App {

    public static void main(String[] args) {
        Despatch despt = ...;
        TFDESP000_ARCH tfdesp000_arch = ...;

        // shorter version
        Contact contact = despt.getDespatchSupplierParty().getParty().getContact();
        setContactField(contact, Contact::getTelephone, Telephone::getValue, tfdesp000_arch::setDcpPartyContactTelephone);
        setContactField(contact, Contact::getTelefax, Telefax::getValue, tfdesp000_arch::setDcpPartyContactTelefax);
        setContactField(contact, Contact::getElectronicMail, EMail::getValue, tfdesp000_arch::setDspPartyContactElectronicMail);
        setContactField(contact, Contact::getName, Name::getValue, tfdesp000_arch::setDspContactName);
    }
    
    private static <T> void setContactField(Contact contact, Function<Contact, T> getField, Function<T,String> getValue, Consumer<String> setter) {
        if (contact != null) {
            T field = getField.apply(contact);
            if (field != null) {
                setter.accept(getValue.apply(field));
            }
        }
    }
}

某些类型在这里可能是错误的,因为您没有给出方法的签名。例如,如果 doesn't always return ,则可以向该方法添加另一个类型参数:getValueStringsetContactField

private static <T,U> void setContactField(Contact contact, Function<Contact, T> getField, Function<T,U> getValue, Consumer<U> setter)
0赞 Geoffrey 7/16/2023 #2

在 set 方法的逻辑中,在此处添加 null 检查。

  • setDcpPartyContactTelephone(...)
  • setDcpPartyContactTelefax(...)
  • setDspPartyContactElectronicMail(...)
  • setDspContactName(..)

示例代码:

public void setDcpPartyContactTelephone(Telephone telephone) {
    if(telephone != null) {
        String value = telephone.getValue();
        //Continue normal operations
    }
}

将联系人声明为变量:

Contact contact = despt.getDespatchSupplierParty().getParty().getContact();

您的完整代码如下所示:

Contact contact = despt.getDespatchSupplierParty().getParty().getContact();
if(contact != null) {
    tfdesp000_arch.setDcpPartyContactTelephone(contact.getTelephone());
    tfdesp000_arch.setDcpPartyContactTelefax(contact.getTelefax());
    tfdesp000_arch.setDspPartyContactElectronicMail(contact.getElectronicMail());
    tfdesp000_arch.setDspContactName(contact.getName());
}

评论

0赞 tgdavies 7/16/2023
问题在于,例如 可能会返回。getTelephone()null
0赞 Geoffrey 7/16/2023
啊,我明白了,那么也许您可以修改 setDcpPartyContactTelephone(...) 方法以将 Telephone 作为参数。我用这个例子更新了我的答案。