Problems to implement convert to selectOneMenu

Asked

Viewed 1,367 times

4

Good afternoon. I’m having trouble implementing a selectOneMenu of objects in my xhtml. Keeps giving "Conversion Error". Could someone tell me the problem of the code below?

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

}

From now on, thank you.


NEW MISTAKE

Something strange happened. I did what the boy above indicated and it worked very well for one case, however, when I made adaptation for another gave problem. Inside the converter, 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 converter="Generic" from p:selectOneMenu, it opens the screen, but not raw, as it gives the conversion error I mentioned above. But when I put it on, it keeps giving Nullpointerexception.

Debugging, I realized that the value parameter is coming null, I just don’t know why. I even made the test of already instantiating PD within the Project class, but it still didn’t work.

Does anyone have any idea what might have happened?

  • On which line is being pointed this conversion error?

  • It’s not being pointed at a line. It’s simply playing this in my <h:messages />

3 answers

2

In selectOneMenu you must add converter="generic", because the convert is not being found.

Your selectOneMenu is 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>

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>
  • My God, I was going to suffer until I found out. kkkk Alexandre, THANK YOU very much!

0

I discovered what the problem was. I had not correctly implemented getId() from the Simpleentity interface. He returned null, which launched the Nullpointerexception.

0

Friend, if you put the notation

@Facesconverter(forClass = Sampleentity.class)

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

convert=""

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

Browser other questions tagged

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