Problem with autocomplete Primefaces

1

I have <p:autoComplete> working, and I have a commandButton that opens dialog so I can make a new one. The dialog opens, I fill in the fields however at the time of saving this exception is thrown:

  

SEVERE: java.lang.NullPointerException at   com.ouvidoria.bean.Converter.getAsObject Subject (SubjectConverter.java:31)

The error happens in the class I use to do the conversion so I can use autoComplete . I believe it is so because clicking on the save button occurred an ajax request and as this field was empty the error occurred.

When I put a process="@this" in the button save the error to but the object that I fill becomes null , for example if in imputText I put value="ouvidoriaBean.ouvidoria.telefone" at the time I click on save this value it arrives null . What could be happening and how can I solve this problem?

Save button

<p:commandButton value="Salvar"
    style="margin-left:28%;margin-top:30px;width:45%"
    actionListener="#{ouvidoriaBean.salvarOuvidoria}" update=":frmPrin" />

Converter Class

@FacesConverter("assunto")
public class AssuntoConverter implements Converter {

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        System.out.println("CODIGO: " + value);
        Integer codigo = null;
        try {
            System.out.println("getAsObject: " + value);
            codigo = Integer.valueOf(value);
        } catch (NumberFormatException e) {
        }

        List<Assunto> lista = new ArrayList<>();
        AssuntoDAO assuntoDAO = new AssuntoDAO();
        lista = assuntoDAO.listar();
        for (Assunto a : lista) {
            if (codigo.equals(a.getCodigo())) {
                return a;

            }
        }

        return null;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if (value != null && !value.equals("")) {
            Assunto assunto = (Assunto) value;
            System.out.println("Testinho: " + assunto);
            return String.valueOf(assunto.getCodigo());
        }
        return null;
    }
}

Method of autoComplete that stays in Bean:

public List<Assunto> sugerirAssunto(String consulta) {
        List<Assunto> assuntosSugeridos = new ArrayList<>();
        List<Assunto> listaAssunto = new ArrayList<>();
        try {

            AssuntoDAO assuntoDAO = new AssuntoDAO();
            listaAssunto = assuntoDAO.listar();
            for (Assunto assunto : listaAssunto) {
                if (assunto.getAssunto().toLowerCase().startsWith(consulta.toLowerCase())) {
                    assuntosSugeridos.add(assunto);
                }
            }
        } catch (Exception e) {
            FacesUtil.adicionarMsgErro("Erro ao listar Assuntos: " + e.getMessage());
        }
        return assuntosSugeridos;
    }

Auto Complete:

<p:autoComplete id="assunto" value="#{ouvidoriaBean.assunto}"
    completeMethod="#{ouvidoriaBean.sugerirAssunto}"
    forceSelection="true" converter="assunto" var="a"
    itemLabel="#{a.assunto}" itemValue="#{a}" />
    
asked by anonymous 28.08.2015 / 14:13

1 answer

1

Techies ,

Separate the dialog you need in another form, and only update it when you need to keep the state of the rest of the page, it would look something like:

<h:form id="formAutoComplete">
<p:autoComplete id="assunto" value="#{ouvidoriaBean.assunto}"
    completeMethod="#{ouvidoriaBean.sugerirAssunto}"
    forceSelection="true" converter="assunto" var="a"
    itemLabel="#{a.assunto}" itemValue="#{a}" />
</h:form>

<h:form id="formDialog">
 //ai você processa apenas este e mantem o resto
 <p:dialog></p:dialog>
</h:form>

Another thing I do is separate the forms , I do not know if this is bad practice, however I separate the responsibilities of who I want to keep the state.

    
28.08.2015 / 15:01