1

As you can see an example in the code below, I need to use a lot of if. Otherwise it throws null pointer exception.

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());
                                    }
                                }

How do I avoid this null pointer exception using few ifs.

neylersin
  • 11
  • 3

2 Answers2

0

You can write a single function which performs the null checks and reuse it:

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));
            }
        }
    }
}

Some of the types may be wrong here, as you haven't given the signatures of your methods. For instance, if getValue doesn't always return String, you can add another type parameter to the setContactField method:

private static <T,U> void setContactField(Contact contact, Function<Contact, T> getField, Function<T,U> getValue, Consumer<U> setter)
tgdavies
  • 10,307
  • 4
  • 35
  • 40
0

In the logic of the set methods, add the null checks there.

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

Example Code:

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

Declare the contact as a variable:

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

Your full code would look like:

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());
}
Geoffrey
  • 1
  • 3
  • The problem is that e.g. `getTelephone()` may return `null`. – tgdavies Jul 16 '23 at 13:22
  • Ah I see, then perhaps you can modify the setDcpPartyContactTelephone(...) method to take the Telephone as a parameter instead. I updated my answer with the example. – Geoffrey Jul 16 '23 at 14:03