I have an object that is injected into my converter:
package br.com.pokemax.modelo.converter;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.inject.Inject;
import br.com.pokemax.modelo.Geracao;
import br.com.pokemax.negocio.GeracaoDAO;
@FacesConverter(value = "geracaoConverter", forClass = Geracao.class)
public class GeracaoConverter implements Converter {
@Inject
GeracaoDAO dao;
@Override
public Object getAsObject(FacesContext arg0, UIComponent arg1, String value) {
if (value != null) {
try {
Geracao geracao = dao.find(Long.parseLong(value));
return geracao;
} catch (Exception e) {
e.printStackTrace();
return e;
}
}
return null;
}
@Override
public String getAsString(FacesContext arg0, UIComponent arg1, Object object) {
if (object != null) {
return String.valueOf(((Geracao) object).getId());
} else {
return null;
}
}
}
I noticed that in other classes this dao is already instantiated, but in the converter it is null and because of that, my code is no more than the line:
Geracao geracao = dao.find(Long.parseLong(value));
And it pops the error NullPointer
, does anyone know how I can solve?
Related question: Value not being written to the bank