0

I am developing a JSF app with Spring. Here is my faces-config.xml file.

<managed-bean>
  <managed-bean-name>loginBean</managed-bean-name>
  <managed-bean-class>com.abc.LoginBean</managed-bean-class>
  <managed-bean-scope>request</managed-bean-scope>
  <managed-property>
    <property-name>userHelper</property-name>
    <property-class>com.abc.MyHelper</property-class>
    <value>#{userHelper}</value>
  </managed-property>
</managed-bean>

In the LoginBean I've a setter and getter method for the property userHelper. But whenever I am trying to access the property userHelper in my LoginBean, I am getting null. What is the cause and how can I solve it?

the below is the stacktrace

17:43:54,325 WARNING [lifecycle] #{loginBean.login}: java.lang.NullPointerException
javax.faces.FacesException: #{loginBean.login}: java.lang.NullPointerException
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118)
    at javax.faces.component.UICommand.broadcast(UICommand.java:315)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)
    at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)
    at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
    at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:598)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    at java.lang.Thread.run(Thread.java:619)
Caused by: javax.faces.el.EvaluationException: java.lang.NullPointerException
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
    ... 27 more
Caused by: java.lang.NullPointerException
    at com.abc.LoginBean.login(LoginBean.java:101)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.el.parser.AstValue.invoke(AstValue.java:170)
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
    ... 28 more
user1536907
  • 21
  • 1
  • 5
  • Can you post the relevant code from `LoginBean`. Also, I take it that `userHelper` is correctly defined as a bean and it is not as `userHelperBean` or something related, am I right? – SJuan76 Nov 22 '12 at 12:00
  • yes , both the classes are bean only. in LoginBean i just have getter and setter methods of username&password – user1536907 Nov 22 '12 at 12:10

1 Answers1

1

Provided that the #{userHelper} itself is absolutely not null, then that can only happen if you were trying to access it already in the constructor of LoginBean. For JSF (and all other dependency injection mechanisms), it's not possible to invoke a setter with the managed property value before the constructor is called. So the property would always be null in the constructor.

Earliest access point would be a @PostConstruct method.

@PostConstruct
public void init() { 
    System.out.println(userHelper); // Look, it should not be null.
}

Do the desired job in there instead of in the constructor.


Unrelated to the concrete problem, a getter is not required for a managed property. I recommend to remove it to prevent accidental usage of this getter in the view.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555