Problems implementing implement convert to selectOneMenu

4

Good afternoon. I am having trouble implementing a selectOneMenu of objects in my xhtml. It is giving "Conversion Error". Could someone tell me what the code problem is?

Error code:

Erro de conversão ao definir o valor 'br.com.somore.model.pojo.Empresa@b54d8861' para 'null Converter'.

Form

<p:selectOneMenu id="empresaMenu" value="#{topsisBean.pd.empresa}" class="componentePF text">
    <f:selectItem itemLabel="Escolha uma Empresa" itemDisabled="true" noSelectionOption="true"/>
    <f:selectItems value="#{empresaBean.empresas}" var="e" itemLabel="#{e.nomeFantasia}" itemValue="#{e}" converter="generic" />
</p:selectOneMenu>

Convert

package br.com.somore.control;

import java.io.Serializable;
import java.util.Map;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

@FacesConverter("generic")
public class GenericConverter implements Converter, Serializable {

    private static final long serialVersionUID = 1L;

    public Object getAsObject(FacesContext ctx, UIComponent component, String value) {
        if (value != null) {
            return this.getAttributesFrom(component).get(value);
        }
        return null;
    }

    public String getAsString(FacesContext ctx, UIComponent component, Object value) {

        if (value != null && !"".equals(value)) {

            SampleEntity entity = (SampleEntity) value;

            // adiciona item como atributo do componente
            this.addAttribute(component, entity);

            Long codigo = entity.getId();
            if (codigo != null) {
                return String.valueOf(codigo);
            }
        }

        return (String) value;
    }

    protected void addAttribute(UIComponent component, SampleEntity o) {
        String key = o.getId().toString(); // codigo da empresa como chave neste caso
        this.getAttributesFrom(component).put(key, o);
    }

    protected Map<String, Object> getAttributesFrom(UIComponent component) {
        return component.getAttributes();
    }
}

Interface

package br.com.somore.control;

public interface SampleEntity {

    Long getId();

}

POJO

package br.com.somore.model.pojo;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import br.com.somore.control.SampleEntity;

@Entity
@Table(name="empresa", schema="somore")
public class Empresa implements Serializable, SampleEntity {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Integer idEmpresa;

    @Column(unique=true)
    private String nomeFantasia;

    @Column(unique=true)
    private String razaoSocial;

    @Column(unique=true)
    private String emailResponsavel;

    @Column(unique=true)
    private long cnpj;

    /***************************************/
    /************** Construtor *************/
    /***************************************/

    public Empresa() {

    }

    /***************************************/
    /********** Getters e Setters **********/
    /***************************************/

    public Integer getIdEmpresa() {
        return idEmpresa;
    }

    public void setIdEmpresa(Integer idEmpresa) {
        this.idEmpresa = idEmpresa;
    }

    public String getNomeFantasia() {
        return nomeFantasia;
    }

    public void setNomeFantasia(String nomeFantasia) {
        this.nomeFantasia = nomeFantasia;
    }

    public String getRazaoSocial() {
        return razaoSocial;
    }

    public void setRazaoSocial(String razaoSocial) {
        this.razaoSocial = razaoSocial;
    }

    public String getEmailResponsavel() {
        return emailResponsavel;
    }

    public void setEmailResponsavel(String emailResponsavel) {
        this.emailResponsavel = emailResponsavel;
    }

    public long getCnpj() {
        return cnpj;
    }

    public void setCnpj(long cnpj) {
        this.cnpj = cnpj;
    }

    /***************************************/
    /************ Hash e Equals ************/
    /***************************************/   

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + (int) (cnpj ^ (cnpj >>> 32));
        result = prime
                * result
                + ((emailResponsavel == null) ? 0 : emailResponsavel.hashCode());
        result = prime * result + ((idEmpresa == null) ? 0 : idEmpresa.hashCode());
        result = prime * result
                + ((nomeFantasia == null) ? 0 : nomeFantasia.hashCode());
        result = prime * result
                + ((razaoSocial == null) ? 0 : razaoSocial.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Empresa other = (Empresa) obj;
        if (cnpj != other.cnpj)
            return false;
        if (emailResponsavel == null) {
            if (other.emailResponsavel != null)
                return false;
        } else if (!emailResponsavel.equals(other.emailResponsavel))
            return false;
        if (idEmpresa == null) {
            if (other.idEmpresa != null)
                return false;
        } else if (!idEmpresa.equals(other.idEmpresa))
            return false;
        if (nomeFantasia == null) {
            if (other.nomeFantasia != null)
                return false;
        } else if (!nomeFantasia.equals(other.nomeFantasia))
            return false;
        if (razaoSocial == null) {
            if (other.razaoSocial != null)
                return false;
        } else if (!razaoSocial.equals(other.razaoSocial))
            return false;
        return true;
    }

    /***************************************/
    /******** Métodos de Interface *********/
    /***************************************/   

    @Override
    public Long getId() {
        return null;
    }

}

Thanks in advance.

NEW ERROR

Something strange happened. I did what the guy above indicated and worked very well for one case, however, when I made adaptation to another gave problem. Inside the convert, the value is coming as null.

SelectOneMenu

<p:selectOneMenu id="pdMenu" converter="generic" value="#{topsisBean.projeto.pd}" class="componentePF text">
    <f:selectItem itemLabel="Escolha um Plano" itemDisabled="true" noSelectionOption="true" />
    <f:selectItems value="#{topsisBean.pds}" var="pd" itemLabel="#{pd.nomePD}" itemValue="#{pd}" converter="generic" />
</p:selectOneMenu>

Converter method

  public String getAsString(FacesContext ctx, UIComponent component, Object value) {
        if (value != null && !"".equals(value)) {
            SampleEntity entity = (SampleEntity) value;

            this.addAttribute(component, entity); // adiciona item como atributo do componente

            Long codigo = entity.getId();
            if (codigo != null)
                return String.valueOf(codigo);
        }
        return null;
    }

When I take the convert="generic" from p: selectOneMenu, it opens the canvas, but not raw because it gives the conversion error I mentioned above. But when I put it in, it gets NullPointerException .

Debugging, I realized that the value parameter is coming null, I just do not know why. I even tested the already instantiating PD within the Project class, but it still did not work.

Does anyone have any idea what might have happened?

    
asked by anonymous 19.05.2015 / 20:51

3 answers

2

In selectOneMenu you should add converter="generic" because the converter is not being found.

Your selectOneMenu looks like this:

<p:selectOneMenu id="empresaMenu" value="#{topsisBean.pd.empresa}" class="componentePF text">
    <f:selectItem itemLabel="Escolha uma Empresa" itemDisabled="true" noSelectionOption="true"/>
    <f:selectItems value="#{empresaBean.empresas}" var="e" itemLabel="#{e.nomeFantasia}" itemValue="#{e}" converter="generic" />
</p:selectOneMenu>

It should look like this:

<p:selectOneMenu id="empresaMenu" converter="generic" value="#{topsisBean.pd.empresa}" class="componentePF text">
    <f:selectItem itemLabel="Escolha uma Empresa" itemDisabled="true" noSelectionOption="true"/>
    <f:selectItems value="#{empresaBean.empresas}" var="e" itemLabel="#{e.nomeFantasia}" itemValue="#{e}" converter="generic" />
</p:selectOneMenu>
    
20.05.2015 / 00:34
0

I found out what the problem was. I had not correctly implemented the getId () of the SimpleEntity interface. It returned null, which threw the NullPointerException.

    
20.05.2015 / 21:33
0

Friend, do you put the notation

  

@FacesConverter (forClass = SampleEntity.class)

before the class declaration would not solve? If you make this change, you do not need to

  

convert="generic"

in the elements of primefaces. Take the test for us there. :)

    
26.05.2015 / 02:56