2 Replies Latest reply on Nov 19, 2009 12:29 AM by German Escobar

    Injected variable sometimes null

    Ole Myhre Newbie

      Seam 2.2.0.GA


      We have a bean that injects a hibernate session object, with @In:




      @In
      private Session hsession;



      This is a convenience var defined in components.xml:


      <factory name="hsession" 
               scope="STATELESS" 
               auto-create="true" 
               value="#{entityManager.delegate}"/>



      This works almost all the time, but some times, we get a nullpointerexception on a line like this:


      hsession.saveOrUpdate(request);



      We cannot reproduce this, but some of our users get this sometimes.... so debugging is hard...


      Stacktrace:




      java.lang.NullPointerException
           at no.deltasoft.scaffolding.RequestBean.saveRequest(RequestBean.java:784)
           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:585)
           at org.jboss.seam.util.Reflections.invoke(Reflections.java:21)
           at org.jboss.seam.intercept.RootInvocationContext.proceed(RootInvocationContext.java:31)
           at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:56)
           at org.jboss.seam.transaction.RollbackInterceptor.aroundInvoke(RollbackInterceptor.java:31)
           at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
           at org.jboss.seam.core.BijectionInterceptor.aroundInvoke(BijectionInterceptor.java:36)
           at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
           at org.jboss.seam.transaction.TransactionInterceptor$1.work(TransactionInterceptor.java:38)
           at org.jboss.seam.util.Work.workInTransaction(Work.java:40)
           at org.jboss.seam.transaction.TransactionInterceptor.aroundInvoke(TransactionInterceptor.java:32)
           at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
           at org.jboss.seam.core.MethodContextInterceptor.aroundInvoke(MethodContextInterceptor.java:42)
           at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
           at org.jboss.seam.intercept.RootInterceptor.invoke(RootInterceptor.java:107)
           at org.jboss.seam.intercept.JavaBeanInterceptor.interceptInvocation(JavaBeanInterceptor.java:166)
           at org.jboss.seam.intercept.JavaBeanInterceptor.invoke(JavaBeanInterceptor.java:102)
           at no.deltasoft.scaffolding.RequestBean_$$_javassist_16.saveRequest(RequestBean_$$_javassist_16.java)
           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:585)
           at org.jboss.el.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:328)
           at org.jboss.el.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:273)
           at org.jboss.el.parser.AstMethodSuffix.getValue(AstMethodSuffix.java:59)
           at org.jboss.el.parser.AstMethodSuffix.invoke(AstMethodSuffix.java:65)
           at org.jboss.el.parser.AstValue.invoke(AstValue.java:96)
           at org.jboss.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
           at com.sun.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:68)
           at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:77






      How is this possible?


      We do not set hsession to anything anywhere in this bean (no reason for us to do so, but we checked just to be sure...).
      So only the @In sets the hsession variable. And we don't get any exceptions about @In cannot inject null-value....
      So how can it be null?


        • 1. Re: Injected variable sometimes null
          Tim Evers Master

          It is very easy for it to be null if you have a scenario like this.


          SeamBeanA with injected Session calls SeamBeanB method. SeamBeanB calles a method on SeamBeanA (to get some data or whatever). At the end of this method call SeamBeanA's injected variables will all be nulled out. So, when control returns back to SeamBeanA all injected variables will be null.


          Because you havn't supplied your whole method I can't tell if this would be a factor for you or not.


          I have raised a JIRA about this issue but the Seam team seem to be ignoring most issues these days and focusing on Weld.


          If this does turn out to be your issue, I'll show you the work around :)

          • 2. Re: Injected variable sometimes null
            German Escobar Newbie

            It's probably a synchronization problem. Annotate your bean with @Synchronized so all requests are serialized.