I have a p: selectOneMenu, with data coming from the database, inside a p: dialog, along with a p: messages and other fields. I want to execute an action (Display some messages) when selecting certain option in p: selectOneMenu, but I get a nullPointerException because the value gets null in the p: ajax listener method. I have already tried to use onchange="submit();"
in p: selectOneMenu, the nullPointerException for, but the method simply does not execute and no exception is shown, simply 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 not Managed Bean
public void onSelectItemMenuChange() {
if(tipoRelatoSelecionado.getDescricao().equals("Acidentes e Danos")) {
FacesUtils.mensErro("Teste Listener");
}
}
Converter
@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");
}
}
}