0

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

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
Thang Pham
  • 38,125
  • 75
  • 201
  • 285

1 Answers1

10

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}" />
Community
  • 1
  • 1
Jörn Horstmann
  • 33,639
  • 11
  • 75
  • 118
  • uhmmm, that is interesting, I will try that. Thank you much :D +1 – Thang Pham Sep 02 '10 at 21:17
  • 1
    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 Sep 02 '10 at 21:32
  • @BalusC: Referring to your comment "it must be dependent on the way how you set the converter in the view". I just tried it with `h:selectOneMenu` and it worked if I use the `converter` attribute as described in the answer above but not if I use ``. Though the converter is annotated with `@FacesConverter` as well. – Matt Handy Jul 21 '11 at 13:28
  • Since JSF 2.3 you can just annotate the converter with @FacesConverter(managed = true) – Max Dec 20 '22 at 07:06