Selected item in p: selectOneMenu gets null in listener

0

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");
    }
}

}

    
asked by anonymous 02.01.2019 / 20:04

2 answers

1

About the first question:

  

The description is not available in @ManagedBean

Evaluating the code of TipoRelatoConverter , in fact the description is not being used:

Long id = Long.parseLong(string); 
TipoRelato tipoRelato = new TipoRelato();
tipoRelato.setIdTipoRelato(id);

In this way only the id of the report is being passed to the return object.

To maintain your entire object you must change the implementation of your converter to save or fetch the converted object again.

In this link you can check out some examples of converters to keep the whole entity. Another example on how to fetch the entity again can be seen in this BalusC post

I believe that implementing the first link is simpler.

Sorry for the delay, I hope I have helped!

    
03.01.2019 / 21:22
0

I believe that in% of% is missing% with%, it is the attribute that will be assigned in the variable f:selectItems , replace with that variable and I believe it will work:

<f:selectItems value="#{livroOrdemController.preencherComboTiposRelatos()}" var="itemTipoRelato" itemValue="#{itemTipoRelato}"/>
    
02.01.2019 / 20:42