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

Asked

Viewed 545 times

0

I am doing a program that registers and relates a customer to a credit card. I am using JSF and Primefaces. It is not necessary to use the Database, so I am storing the data in a same Arraylist.

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

In the Card Registration screen, in addition to the data to be registered, I put one where shows the results of an Arraylist with the name of all registered customers. The problem is that when I try to create the card with a particular registered customer gives this error here:

inserir a descrição da imagem aqui

I tried to use some converters too, but unsuccessfully and am not sure if this is the problem. Below are some snippets of my code.

Cartaobean class:

public class CartaoBean implements Serializable {

/**
 * 
 */
private static final long serialVersionUID = -8764412118399727943L;

private Cartao cartao;

private ArrayList<Cartao> cartoes;

private Boolean mostrarPainel;

private Cliente titular;

public CartaoBean() {
    this.mostrarPainel = true;
    this.cartao = new Cartao();
    this.cartoes = new ArrayList<Cartao>();
}

public void cadastrar() {

    this.cartoes.add(this.cartao);
    this.cartao = new Cartao();

    FacesMessage m = new FacesMessage();
    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;
}

}

Class Card

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 xhtml of the register

<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>

1 answer

1

you have to implement a convert (conversion) class to follow an example along with 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> 

Personal

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;
        }
    }

}

Browser other questions tagged

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