Null Pointer Exception in ItemPropertyChangeListener

Hi,

I have set a ItemPropertyChangeListener for a datasource’s related Entity attribute on a Edit screen (PickerField). It is working fine except for the scenario where the user selected the same related Entity from the related entity’s lookup screen. The problem is i got “nullpointerexception” in the listener code after returning from the lookup screen.

Seem like the listener is being triggered twice under this scenario - first time it changed the related entity attribute in the datasource to null (which causes the “nullpointerexception”) and then a second time it changed back to the original value. I couldn’t figure it out to solve the problem. Would much appreciate if anyone can help me out . Thanks in advance.

Attaching the screen controller codes as below:

public class ClientEdit extends AbstractEditor<Client> {

    @Inject
    private Datasource<Client> clientDs;
    @Inject
    private Datasource<Party> partyDs;

    @Named("fieldGroup.party")
    private LookupPickerField partyField;

    @Named("fieldgroup_1.clientNo")
    private TextField clientNoField;

    @Override
    public void init(Map<String, Object> params) {
        partyField.addLookupAction();

        clientDs.addItemPropertyChangeListener(e -> {
            if ("party".equals(e.getProperty())) {
                if (clientDs.getItem().getParty().getPartyType() == PartyType.Individual) {
                    clientDs.getItem().setClientNo(partyDs.getItem().getPassportNo());
                }
            }
        });
    }
}

Hi,

For now PickerField LookupAction calls setValue() method twice (with null value and with the selected) if the new item has the same id as current to ensure that previous item will be replaced with the new.

It is a known API issue and we will try to change it may be in the next major release of the platform, but now you have to check for null e.getValue() and entity attribute that is bound to a PickerField. In your case it is clientDs.getItem().getParty().

Thanks for the explanation Yuriy. Look forward to the improvement in next release.

For now, i check for null first to avoid the runtime error.

Best regards,
David See

:ticket: See the following issue in our bug tracker:

https://youtrack.cuba-platform.com/issue/PL-8152