views:

35

answers:

2

This converter is called from my JSF. I already register it inside faces-config.xml

public class ProjectConverter implements Converter{

    @EJB
    DocumentSBean sBean;

    @ManagedProperty(value="#{logging}")
    private Logging log;    

    public ProjectConverter(){
    }

    public Object getAsObject(FacesContext context, UIComponent component, String value) 
    {
        if(value.trim().equals("")){
            return null;
        }
        return sBean.getProjectById(value);

    }

    public String getAsString(FacesContext context, UIComponent component, Object value) 
    {
        if(value == null){
            return null;
        }
        return String.valueOf(((Project) value).getId());
    }
}

I ran into java.lang.NullPointerException, when I am in getAsObject(), the primary reason is because my Session Bean sBean is null. I dont know how to fix this, I need to access to my Session bean so that I can query from my database

+1  A: 

In JSF, only a @ManagedBean is eligible for resource injection. So @EJB (and @ManagedProperty) in a @FacesConverter and so on won't work.

You need to manually retrieve it from the InitialContext.

See also:

BalusC
I cant seem to get the JNDI lookup correctly. So my EJB is inside `Documents-ejb.jar`, and the package is `org.xdrawings.ejb`, then my JNDI lookup is like this: `java:global/Documents-ejb/DocumentSBean!org.xdrawings.ejb.DocumentSBean`, is this correct?
Harry Pham
+2  A: 

As BalusC said, injection only works in managed beans. You can however declare your converter as a managed bean in your faces-config

<managed-bean>
   <managed-bean-name>myConverter</managed-bean-name>
   <managed-bean-class>com.example.MyConverter</managed-bean-class>
   <managed-bean-scope>request</managed-bean-scope>
</managed-bean>

And later reference it in a jsf component with an el expression:

<h:outputText value="#{myBean.value}" converter="#{myConverter}" />
Jörn Horstmann
uhmmm, that is interesting, I will try that. Thank you much :D +1
Harry Pham
@Jorn: did it work to your experience? I haven't tried it (no EJB here), however this should in *theory* indeed work, but but I've seen resources confirming that this didn't work. Or it must be dependent on the way how you set the converter in the view.
BalusC
@BalusC: This actually works. I test it, and it actually work for me.
Harry Pham
Great, good to know :)
BalusC