1

I have the following JSF2 controller / bb which i have injected a session bean. However, at it is unable to get a reference to the EJB Local Session Bean.

JSF2 Backing Bean:

@ManagedBean
@Named("scorecardBackingBean")
@SessionScoped
public class ScorecardBackingBean implements Serializable {
    private static final long serialVersionUID = 7231502115861150753L;

    @Inject
    private IScorecardServiceBean scorecardServiceBean;

    @PostConstruct
    private void prepareMainLineChart() {
        this.mainLineChart = new CartesianChartModel();


         List<OrderVolume> orderVolumeList = this.scorecardServiceBean
         .getOrderVolume(); //ERROR here, scorecardServiceBean is null.
}

EJB:

@Stateless
public class ScorecardServiceBean implements IScorecardServiceBean {
}

EJB Interface:

@Local
public interface IScorecardServiceBean {
    List<OrderVolume> getOrderVolume();
}

Error encountered:

java.lang.IllegalStateException: JBAS011048: Failed to construct component instance
    at org.jboss.as.ee.component.BasicComponent.constructComponentInstance(BasicComponent.java:163)
    at org.jboss.as.ee.component.BasicComponent.createInstance(BasicComponent.java:95)
    at org.jboss.as.web.deployment.component.WebComponentInstantiator$2.<init>(WebComponentInstantiator.java:96)
    at org.jboss.as.web.deployment.component.WebComponentInstantiator.initializeInstance(WebComponentInstantiator.java:94)
.................
Caused by: java.lang.NullPointerException
    at sg.java.bus.controller.scorecard.ScorecardBackingBean.prepareMainLineChart(ScorecardBackingBean.java:79)
    at sg.java.bus.controller.scorecard.ScorecardBackingBean.init(ScorecardBackingBean.java:61)
    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.jboss.as.ee.component.ManagedReferenceLifecycleMethodInterceptorFactory$ManagedReferenceLifecycleMethodInterceptor.processInvocation(ManagedReferenceLifecycleMethodInterceptorFactory.java:130)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
    at org.jboss.invocation.WeavedInterceptor.processInvocation(WeavedInterceptor.java:53)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
    at org.jboss.as.ee.component.ManagedReferenceInterceptorFactory$ManagedReferenceInterceptor.processInvocation(ManagedReferenceInterceptorFactory.java:106)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
    at org.jboss.invocation.WeavedInterceptor.processInvocation(WeavedInterceptor.java:53)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
    at org.jboss.as.ee.component.TCCLInterceptor.processInvocation(TCCLInterceptor.java:45)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
    at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61)
    at org.jboss.as.ee.component.BasicComponent.constructComponentInstance(BasicComponent.java:161)
    ... 52 more
Oh Chin Boon
  • 23,028
  • 51
  • 143
  • 215

1 Answers1

1

You are mixing CDI managed beans (@Named) and JSF managed beans (@ManagedBean). Only one of these annotations is necessary.

If you choose the one or the other, make sure that you import the correct scope class.

CDI: javax.enterprise.context.SessionScoped

JSF: javax.faces.bean.SessionScoped

See these related questions & answers for background information:

Community
  • 1
  • 1
Matt Handy
  • 29,855
  • 2
  • 89
  • 112
  • Thanks Matt. I have taken out redundant "@Named("scorecardBackingBean")" but the injected resource is an EJB session bean. It is still giving the same esception. – Oh Chin Boon Jul 23 '12 at 09:47
  • I have changed from @Inject to EJB and it worked. I need to check out on that, why. But thanks a whole lot. – Oh Chin Boon Jul 23 '12 at 10:08
  • @Chin That's indeed strange. Both injection methods should work (from theory), maybe you get some explaination in the third link I added to my answer. (I just looked into my latest project and I use `@Inject` for @Named beans and @EJB for the injection of ejbs). – Matt Handy Jul 23 '12 at 12:49
  • Yes, in fact I had two projects, one using Inject, one using EJB, difference is, one is managed by Maven build, one is custom EAR'ed, this one is Maven. – Oh Chin Boon Jul 23 '12 at 13:31