0

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.

jMarcel
  • 865
  • 5
  • 24
  • 48
  • Debug and check the value of `this.getSelected().getDtInicial()` I would take a stab and guess that's returning null, you are then trying to operate on a null thus causing a null pointer – Luke Garrigan Jul 20 '18 at 21:29
  • Luke Garrigan, I knew it was something very simple I was missing, you're right! Thanks! – jMarcel Jul 20 '18 at 21:37
  • Please, post your explanation as an answer so I can accept it as correct. – jMarcel Jul 20 '18 at 21:39

1 Answers1

0

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.

Luke Garrigan
  • 2,141
  • 10
  • 20
  • I wasn't focusing on `this.getSelected().getDtInicial()`, but in the object to set. Thank you so much!! – jMarcel Jul 20 '18 at 21:45