简体   繁体   中英

How To Set A Value To An Object In An (Actionlistener) Save Method?

I have an actionListener below that works fine:

<p:commandButton actionListener="#{demandasController.saveNew}" ... />

The saveNew method is:

public void saveNew(ActionEvent event) {

    Situacao situacao = new Situacao();
    situacao.setIdSituacao(4);

    this.getSelected().setSituacao(situacao);

    super.saveNew(event);
}

If I use the code above, it works just fine. But if I try...

public void saveNew(ActionEvent event) {

Situacao situacao = new Situacao();

Date currentDate = new Date();

if (this.getSelected().getDtInicial().after(currentDate)) {
    situacao.setIdSituacao(4);
} else {
    situacao.setIdSituacao(5);
}

this.getSelected().setSituacao(situacao);

super.saveNew(event);
}

...it returns a NullPointerException .

Does anyone knows why this is happening and/or how can I fix it ?

Thanks in advance.

Judging from the error message: NullPointerException and from the initial code working but the second lot not working, the following code must be returning null.

this.getSelected().getDtInicial()

then the .after(currentDate) is an operation on a null which causes the null pointer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM