Item selected in p:selectOneMenu reaches null in Systener

Asked

Viewed 148 times

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 a form?

  • It is. The page has a form involving several p:dialogs and at Submit time I process the fields I want to submit

  • Have you checked whether the change your convert is working as expected? Please update the question with the convert code if possible

  • I edited the question with Converter

  • And when debugging, it returns the expected result, or is not even called?

  • The converter is called yes. When debugging I realized that only the description is coming null, the id is not

  • 1

    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.

  • If I debug in the Setter of typeRelatoSelected it comes null.

  • In p:selectOneMenu appears the description of the String type, which is String, but it does not go to typeRelated, only id will

  • You couldn’t compare Id instead of String?

  • 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

  • I will review your convert in a little while, soon put here

  • In your selectOneMenu you put the attribute immediate="true". If it works I’ll explain when I arrive.

  • @Douglas, it didn’t work out.

Show 9 more comments

2 answers

0


On the first question:

Description not available on @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);

This way only the report id is being passed to the return object.

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

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

I believe that the implementation of the first link is simpler.

Sorry for the delay, I hope I’ve helped!

  • Thank you, I managed to solve.

0

I believe in your f:selectItemslack the itemValue, it is the attribute that will be assigned in the variable livroOrdemController.tipoRelatoSelecionado, replace with this and I believe it will work:

<f:selectItems value="#{livroOrdemController.preencherComboTiposRelatos()}" var="itemTipoRelato" itemValue="#{itemTipoRelato}"/>
  • I made the changes, but it stayed the same

Browser other questions tagged

You are not signed in. Login or sign up in order to post.