Problem with Primefaces autocomplete

Asked

Viewed 555 times

1

I have a <p:autoComplete> working, and I have a commandButton that opens a dialog so that I can make a new registration. The dialog opens, I fill the fields but when saving this exception is launched:

GRAVE: java.lang.Nullpointerexception at com.ouvidoria.bean.AssuntoConverter.getAsObject(Assuntoconverter.java:31)

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

When I put a process="@this" on the button save the error to however the object I fill is null, for example if in the imputText place the value="ouvidoriaBean.ouvidoria.telefone" when I click to save this value arrives null. What may be happening and how I can 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 which is 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}" />

1 answer

1


Techies,

Separates the dialog that you need in another form, and only update it when you need it while maintaining 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 don’t know if this is bad practice, but I separate the responsibilities of who I want to keep the state.

Browser other questions tagged

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