Question

I have a list of governorates that I displayed in a <p:selectOneMenu>

the java code in the managed bean:

public List<SelectItem> gouvernorats() {
    List<Gouvernorat> all = emetteursEJB.findAllGouvernorat();
    List<SelectItem> items = new ArrayList<>();
    for (Gouvernorat g : all) {
       items.add(new SelectItem(g, g.getLibelleGouv()));
    }
    return items;
}

in the <p:selectOneMenu> I add <p:ajax>:

<p:selectOneMenu value="#{emetteurBean.selectedGouvernorat}" style="width:160px"  >  
    <f:selectItem itemLabel="Select One" itemValue="" />  
    <f:selectItems value="#{emetteurBean.gouvernorats()}" />  
    <p:ajax event="change" listener="#{emetteursBean.handelGouvChanged()"/>
</p:selectOneMenu>

in the method handelGouvChanged() selectedGouvernorat object is always null;

Recently I added the convert I stumbled upon NullPointerException

@FacesConverter(forClass = Gouvernorat.class)
public class EmetteursConverter implements Converter {

    @EJB
    private ReferentielDaoLocal refEJB;

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String selectedValue) {
        System.out.println("Inside The Converter");
        System.out.println(selectedValue.length());
        if (selectedValue == null) {
            return null;
        } else {
            return refEJB.findGouvByCode(selectedValue.trim());
        }
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if (value == null) {
            return null;
        } else {
            return String.valueOf(((Gouvernorat) value).getIdGouvernorat());
        }
    }
}
Was it helpful?

Solution

I found a solution, I injected EJB with InitilContext.lookup ()

@FacesConverter(forClass = Gouvernorat.class)
public class GouvernoratConverter implements Converter {

    private static final Logger LOG = Logger.getLogger(GouvernoratConverter.class.getName());

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

        if (selectedValue == null) {
            return null;
        } else {
            try {
                ReferentielDaoLocal myService = (ReferentielDaoLocal) new     
                InitialContext().lookup("java:global/ErpCCF/ErpCCF-ejb/ReferentielDaoImpl");
                return myService.findGouvByCode(selectedValue);
            } catch (NamingException ex) {
                LOG.log(Level.SEVERE, "Converter Gouvernorat Error", ex.getMessage());
                return null;
            }
        }
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if (value == null) {
            return null;
        } else {
            return String.valueOf(((Gouvernorat) value).getIdGouvernorat());
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top