0
I have a p:selectOneMenu, with data coming from the database, within a p:dialog, along with a p:messages and other fields. I want to perform an action (Display some messages) when selecting a certain option in p:selectOneMenu, but I get a nullPointerException because the value arrives null in the Listener method of p:ajax. I’ve tried using the onchange="submit();"
in p:selectOneMenu, nullPointerException stops, but the method simply does not execute and no Exception is shown, just nothing happens.
Dialog
<p:dialog id="dialogRelato"
showEffect="fade" hideEffect="fade"
modal="true" header="Novo Relato"
widgetVar="dialogRelato" minHeight="40"
resizable="false">
<p:messages id="menssagens" autoUpdate="true" closable="true" />
<b>
<h:outputLabel value="Tipo de Relato: " />
</b>
<p:selectOneMenu style="height: 20px; width: 200px;"
value="#{livroOrdemController.tipoRelatoSelecionado}"
converter="tipoRelatoConverter">
<f:selectItems value="#{livroOrdemController.preencherComboTiposRelatos()}"/>
<p:ajax event="change" listener="#{livroOrdemController.onSelectItemMenuChange()}" process="@this" />
</p:selectOneMenu>
<br />
<br />
<p:calendar id="calendarDataOcorrencia"
pattern="dd/MM/yyyy"
locale="pt_BR"
showOn="button"
value="#{livroOrdemController.dataOcorrencia}">
<p:ajax event="dateSelect" listener="#{livroOrdemController.onDataOcorrenciaSelect()}"/>
</p:calendar>
</p:dialog>
Method in Managed Bean
public void onSelectItemMenuChange() {
if(tipoRelatoSelecionado.getDescricao().equals("Acidentes e Danos")) {
FacesUtils.mensErro("Teste Listener");
}
}
Convert
@FacesConverter(value="tipoRelatoConverter")
public class TipoRelatoConverter implements Converter {
public TipoRelatoConverter() {
}
@Override
public Object getAsObject(FacesContext context, UIComponent component, String string) {
if (string == null || string.equals("Selecione...")) {
return null;
}
Long id = Long.parseLong(string);
TipoRelato tipoRelato = new TipoRelato();
tipoRelato.setIdTipoRelato(id);
return tipoRelato;
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object object) {
if (object == null) {
return null;
}
if(object instanceof TipoRelato) {
TipoRelato tipoRelato = (TipoRelato) object;
return "" + tipoRelato.getIdTipoRelato();
}
else {
throw new IllegalArgumentException("object:" + object + "of type:" +
object.getClass().getName() + "; expected type:br.org.web.entities.TipoRelato");
}
}
}
To
p:dialog
is inside aform
?– nullptr
It is. The page has a form involving several p:dialogs and at Submit time I process the fields I want to submit
– user70765
Have you checked whether the
change
your convert is working as expected? Please update the question with the convert code if possible– nullptr
I edited the question with Converter
– user70765
And when debugging, it returns the expected result, or is not even called?
– nullptr
The converter is called yes. When debugging I realized that only the description is coming null, the id is not
– user70765
Also debug inside your Managed Bean’s Setter to check that it is not being called twice, once with the correct value and once with the null value.
– nullptr
If I debug in the Setter of typeRelatoSelected it comes null.
– user70765
In p:selectOneMenu appears the description of the String type, which is String, but it does not go to typeRelated, only id will
– user70765
You couldn’t compare Id instead of String?
– nullptr
I’m already doing this, I gave up trying for the string, but I didn’t quite understand why this happens. If it was to be like this, or is it a bug
– user70765
I will review your convert in a little while, soon put here
– nullptr
In your selectOneMenu you put the attribute immediate="true". If it works I’ll explain when I arrive.
– Roknauta
@Douglas, it didn’t work out.
– user70765