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.
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)