Error in converting JSF

3

Error or Bug? Hi, I have an error appearing on the eclipse console, on the registration screen the form writes 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 where I use 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 to this line of error.

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

I do not know if it's really a bug or a bug, thanks in advance.

    
asked by anonymous 14.05.2015 / 21:54

2 answers

2

Your getAsObject method is taking the string id , which in this case has a value of "Select" and is trying to give Long.valueof("Selecione") this causes a Exception . If you do not want exception to test, if the id value is equal to "Select" and treat this as desired, as long as the return is null . >     

19.05.2015 / 22:54
1

If you are using this converter in a selectOneMenu, follow a code example:

<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>
    
15.05.2015 / 15:53