Doubt Converter and CDI

Asked

Viewed 164 times

1

I’m a few hours testing and trying to understand why this code here isn’t working and I’m getting one NullPointerExcepetion . Someone can sense something wrong ?

    import javax.enterprise.context.RequestScoped;
    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.Inject;
    import javax.inject.Named;

    import modelo.Condomino;
    import repositorio.Condominos;

    @Named
    @RequestScoped
    public class CondominoConverter implements Converter{

    @Inject
    private Condominos condominos;

@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {

    if(!value.equals(null)){
        Condomino condomino = condominos.getCondominoPorCPF(value);

        if (condomino == null)
            throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR,"Não existe","Não existe"));

        return  condomino;
    }
    return null;

}

@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {

    if (null == value || !(value instanceof Condomino))
        return null;
    Condomino condomino = (Condomino) value;

    return condomino.getCPF()==null?null:condomino.getCPF().toString();

}

}

  • 1

    where the nullpointer is?

  • continued checking and when I use the save this happening two requests should be why I’m getting a nullpointerexcepetion :(

  • which line is bursting the NullPointerException

1 answer

1


I was able to solve my problem as follows knowing that I am using version 2.2.8-02 of jsf, I thought the solution in the convert using @Named would work but it didn’t work out so well, so I followed a tip I saw in a Algaworks class letting my convert as follows:

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.faces.convert.FacesConverter;
import modelo.Condomino;
import repositorio.Condominos;
import util.CDIServiceLocator;

@FacesConverter(forClass=Condomino.class)
public class CondominoConverter implements Converter{       

    private Condominos condominos;

    public CondominoConverter() {
        condominos =  CDIServiceLocator.getBean(Condominos.class);
    }

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {

        if(!value.equals(null)){
            Condomino condomino = condominos.getCondominoPorCPF(value);

            if (condomino == null)
                throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR,"Não existe","Não existe"));

            return  condomino;
        }
        return null;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {

        if (null == value || !(value instanceof Condomino))
            return null;
        Condomino condomino = (Condomino) value;

        return condomino.getCPF()==null?null:condomino.getCPF().toString();
    }


}

The Cdiservicelocator class is instantiating the object directly in its constructor, since by the dependency injection was calling twice and losing the reference returning me always null.

import java.util.Set;

import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class CDIServiceLocator {

    private static BeanManager getBeanManager() {
        try {
            InitialContext initialContext = new InitialContext();
            return (BeanManager) initialContext.lookup("java:comp/env/BeanManager");
        } catch (NamingException e) {
            throw new RuntimeException("Não pôde encontrar BeanManager no JNDI.");
        }
    }

    @SuppressWarnings("unchecked")
    public static <T> T getBean(Class<T> clazz) {
        BeanManager bm = getBeanManager();
        Set<Bean<?>> beans = (Set<Bean<?>>) bm.getBeans(clazz);

        if (beans == null || beans.isEmpty()) {
            return null;
        }

        Bean<T> bean = (Bean<T>) beans.iterator().next();

        CreationalContext<T> ctx = bm.createCreationalContext(bean);
        T o = (T) bm.getReference(bean, clazz, ctx);

        return o;
    }

}

Browser other questions tagged

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