Conversion error when setting value '' to 'null Converter'. - JSF

0

I am doing a program that registers and relates a customer to a credit card. I'm using JSF and PrimeFaces. It is not necessary to use the Database, so I am storing the data in an ArrayList myself.

Basically, when creating a new card I need to show all the registered clients so that I can select which customer the card will be.

On the Card Registration screen, in addition to the data to be registered, I placed one where it shows the results of an ArrayList with the name of all the registered clients. The problem is that when I try to create the card with a certain registered customer it gives this error here:

I'vetriedusingsomeconverterstoo,buttonoavailandI'mnotsureifthat'stheproblem.Belowaresomeexcerptsfrommycode.

TarjetaBeanclass:

publicclassCartaoBeanimplementsSerializable{/****/privatestaticfinallongserialVersionUID=-8764412118399727943L;privateCartaocartao;privateArrayList<Cartao>cartoes;privateBooleanmostrarPainel;privateClientetitular;publicCartaoBean(){this.mostrarPainel=true;this.cartao=newCartao();this.cartoes=newArrayList<Cartao>();}publicvoidcadastrar(){this.cartoes.add(this.cartao);this.cartao=newCartao();FacesMessagem=newFacesMessage();m.setSummary("Cartão cadastrado com sucesso!");
    m.setSeverity(FacesMessage.SEVERITY_INFO);
    /**
     * Colocando a mensagem na lista
     */
    FacesContext.getCurrentInstance().addMessage(null, m);
}

public Cartao getCartao() {
    return cartao;
}

public void setCartao(Cartao cartao) {
    this.cartao = cartao;
}

public ArrayList<Cartao> getCartoes() {
    return cartoes;
}

public void setCartoes(ArrayList<Cartao> cartoes) {
    this.cartoes = cartoes;
}

public Boolean getMostrarPainel() {
    return mostrarPainel;
}

public void setMostrarPainel(Boolean mostrarPainel) {
    this.mostrarPainel = mostrarPainel;
}

public Cliente getTitular() {
    return titular;
}

public void setTitular(Cliente titular) {
    this.titular = titular;
}

}

Card Class

public class Cartao implements Serializable {


private static final long serialVersionUID = -4746760068026694731L;

@NotBlank
private String numero;

@NotNull
private double creditoInicial;

@NotNull
private Cliente titular;

public Cartao() {

}

public Cartao(String numero, double creditoInicial) {

    this.numero = numero;
    this.creditoInicial = creditoInicial;

}

public String getNumero() {
    return numero;
}

public void setNumero(String numero) {
    this.numero = numero;
}

public Cliente getTitular() {
    if (titular == null) titular = new Cliente();
    return titular;
}

public void setTitular(Cliente titular) {
    this.titular = titular;
}


public double getCreditoInicial() {
    return creditoInicial;
}

public void setCreditoInicial(double creditoInicial) {
    this.creditoInicial = creditoInicial;
}

}

The registration xhtml

<h:body>

<h:form>

    <p:growl globalOnly="true" />

    <p:panel header="Cadastro de Cartão" toggleable="true" closable="true" rendered="#{CartaoBean.cartao.numero}" />

    <h:panelGrid>

    <p:outputLabel for="n1" value="Cliente"/>
    <p:selectOneMenu id="n1" value="#{clienteBean.cliente.cartao}">
        <f:selectItems value="#{clienteBean.clientes}" var="cliente" itemLabel="#{cliente.nome}" />
    </p:selectOneMenu>      
    <p:message for="n1" />

    <p:outputLabel for="n2" value="Crédito Inicial"/>
    <p:inputText id="n2" value="#{cartaoBean.cartao.creditoInicial}"/>
    <p:message for="n2" />


    <f:facet name="footer">
        <p:commandButton update="@form" action="#{cartaoBean.cadastrar}" value="Gerar Cartão de Crédito"/>
    </f:facet>

    </h:panelGrid>

</h:form>

    
asked by anonymous 20.11.2018 / 17:57

1 answer

1

You have to implement a class of conversion (conversion) following an example next to your p:selectOneMenu

<p:selectOneMenu id="n1" value="#{clienteBean.cliente.cartao}" converter="ClienteConversor" >
        <f:selectItems value="#{clienteBean.clientes}" var="cliente" itemLabel="#{cliente.nome}" />
    </p:selectOneMenu> 

PersonConverte

package org.salomao.conversor;

import java.io.Serializable;

import javax.enterprise.context.ApplicationScoped;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.inject.Named;

import org.salomao.model.cliente;

@Named
@ApplicationScoped
public class ClienteConversor implements Converter, Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public ClienteConversor() {
        // TODO Auto-generated constructor stub
    }

    public Object getAsObject(FacesContext fc, UIComponent uic, String value) {
        if (value != null && value.trim().length() > 0) {
            try {
                return new ClienteDao().findById(Integer.valueOf(value));

            } catch (Exception e) {
                System.out.println(e.getMessage());
                throw new ConverterException(
                        new FacesMessage(FacesMessage.SEVERITY_ERROR, "Conversion Error", "cliente não valida."));
            }
        } else {
            return null;
        }
    }

    public String getAsString(FacesContext fc, UIComponent uic, Object object) {
        if (object != null) {
            return ((Cliente) object).getId();
        } else {
            return null;
        }
    }

}
    
21.11.2018 / 11:57