Error in converting JSF

Asked

Viewed 728 times

3

Error or Bug? Well I have an error appearing in the eclipse console, on the registration screen the form records perfectly, everything ok, but the console points:

java.lang.NumberFormatException: For input string: "Selecione"
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at br.com.bb.uds.rotinas.controller.converter.PessoaConverter.getAsObject(PessoaConverter.java:18)
    at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getConvertedValue(HtmlBasicInputRenderer.java:171)
    at com.sun.faces.renderkit.html_basic.MenuRenderer.convertSelectOneValue(MenuRenderer.java:202)

My convert:

@FacesConverter(value = "pessoaConverter", forClass = Pessoa.class)
public class PessoaConverter implements Converter {

    @Override
    public Object getAsObject(FacesContext arg0, UIComponent arg1, String id) {
        try {
            PessoaServices pessoaServices = new PessoaServices();
            return pessoaServices.obterEntidade(Long.valueOf(id));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public String getAsString(FacesContext arg0, UIComponent arg1, Object pessoa) {
        if (pessoa.toString().equals("-1")) {
            return null;
        }
        return ((Pessoa) pessoa).getId().toString();
    }

}

XHTML in which I use the convert:

<!-- RESPONSAVEL -->
<h:outputLabel for="responsavel" value="Responsável"
    styleClass="panelGridGerenciar" />
<p:selectOneMenu id="responsavel"
    value="#{rotinasBean.rotina.pessoaResponsavel}" required="true"
    requiredMessage="Selecione um responsável"
    converter="pessoaConverter">
    <p:ajax event="change" listener="#{rotinasBean.changeSubstituto}"
        update="substituto" />
    <f:selectItem itemLabel="Selecione" itemValue="-1" />
    <f:selectItems var="_responsavel"
        value="#{rotinasBean.selectResponsaveis}"
        itemValue="#{_responsavel}" itemLabel="#{_responsavel.nome}" />
</p:selectOneMenu>

The console points that line of error.

return pessoaServices.obterEntidade(Long.valueOf(id));

I don’t know if it’s really a mistake or a bug.

  • You have already debugged to see the values that are passed to the variables?

2 answers

2


His method getAsObject is taking the string id, which in this case has the value "Select" and is trying to give a Long.valueof("Selecione") that causes a Exception. If you don’t want it to happen to Exception test if the value id is equal to "Select" and treat it as you wish, as long as the return is null.

  • Beauty, thanks, really just went to do if the id == "select" and the error in the console does not appear anymore. Thanks.

1

If you are using this convert in a selectOneMenu, follow an example of code:

<p:selectOneMenu  value="#{bean.lista}" converter="pessoaConverter">
        <f:selectItem itemLabel="Selecione" itemValue="#{null}"/>
        <f:selectItems value="#{listapessoas}" var="p" itemValue="#{p}" itemLabel="#{p.nome}"/>
    </p:selectOneMenu>
  • Rafael and in a selectOneMenu even, but I posted my ta equal to your example and continues with this error.

  • I would remove this itemValue="-1" and leave null. So the problem is in your getAsObject method. In the showcase of the primefaces has an example of convert, I made my based on it.

Browser other questions tagged

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