Bom dia, alguem poderia me ajudar resolver esse erro tenho a mesma funcionalidade em outro projeto e nao apresenta erro ao tentar fazer a conversão pelo que eu percebi ao debugar o erro esta dando porque esta tentando converter um objeto com os atributos nullo mas eu nao tenho esse objeto em nenhum lugar nem no banco de dados.
GitHub: https://github.com/jlfjunior/Livraria.git
Pagina:
<p:selectOneMenu value="#{livroBeans.livro.tipo}" converter=“convert”>
<f:selectItem noSelectionOption=“true” itemLabel=“Select One” />
<f:selectItems value="#{tipoBeans.listaDeTipos}" var=“tipo"
itemLabel=”#{tipo.descricao}" itemValue="#{tipo}" />
</p:selectOneMenu>
ConvertorGenerico
package br.com.livraria.utils;
import br.com.livraria.models.GenericEntity;
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(“convert”)
public class GenericConverter implements Converter {
@Override
public Object getAsObject(FacesContext ctx, UIComponent component, String value) {
if (value != null) {
return this.getAttributesFrom(component).get(value);
}
return null;
}
@Override
public String getAsString(FacesContext ctx, UIComponent component, Object value) {
if (value != null && !"".equals(value)) {
GenericEntity entity = (GenericEntity) value;
this.addAttribute(component, entity);
Long codigo = entity.getId();
if (codigo != null) {
return String.valueOf(codigo);
}
}
return (String) value;
}
protected void addAttribute(UIComponent component, GenericEntity o) {
String key = o.getId().toString();
this.getAttributesFrom(component).put(key, o);
}
protected Map<String, Object> getAttributesFrom(UIComponent component) {
return component.getAttributes();
}
}
Classe Livro:
package br.com.livraria.models;
import java.util.Date;
import java.util.Objects;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity(name = “Livros”)
public class Livro implements GenericEntity {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
private String titulo;
private String autores;
private Date anoDate = new Date();
private String cidade;
private String resumo;
private Assunto assunto = new Assunto();
private Tipo tipo = new Tipo();
@Override
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitulo() {
return titulo;
}
public void setTitulo(String titulo) {
this.titulo = titulo;
}
public String getAutores() {
return autores;
}
public void setAutores(String autores) {
this.autores = autores;
}
public String getCidade() {
return cidade;
}
public void setCidade(String cidade) {
this.cidade = cidade;
}
public String getResumo() {
return resumo;
}
public void setResumo(String resumo) {
this.resumo = resumo;
}
public Date getAno() {
return anoDate;
}
public void setAno(Date ano) {
this.anoDate = ano;
}
public Assunto getAssunto() {
return assunto;
}
public void setAssunto(Assunto assunto) {
this.assunto = assunto;
}
public Tipo getTipo() {
return tipo;
}
public void setTipo(Tipo tipo) {
this.tipo = tipo;
}
@Override
public int hashCode() {
int hash = 3;
hash = 17 * hash + Objects.hashCode(this.id);
return hash;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Livro other = (Livro) obj;
if (!Objects.equals(this.id, other.id)) {
return false;
}
return true;
}
}