Announcement Announcement Module
Collapse
No announcement yet.
Autowired objects null? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Autowired objects null?

    I have a class similar to this (extraneous code snipped):
    Code:
    package com.mycompany.myproj;
    
    import java.util.List;
    import com.mycompany.persistence.dao.myStuffDao;
    import com.mycompany.persistence.model.myStuff;
    import org.springframework.beans.factory.annotation.Autowired;
    
    public class MyThread implements Runnable {
        @Autowired
        private MyStuffDao myStuffDao;
    
        public void run() {
            try
            {
                List<myStuff> stuffList = 
                    (List<myStuff>) myStuffDao.findAll();
                // I would do more stuff here, but myStuffDao is null.
            } catch (Exception e) {
                log.error("Oops: " + e.getMessage());
            }
        }    
    }
    I have a spring XML file that includes the following in the beans block:
    Code:
      <context:component-scan base-package="com.mycompany.myproj"/>
      <context:annotation-config />
      <bean id="app" class="com.mycompany.myproj.App"></bean>
    App gets kicked off by Tomcat, which then runs MyThread.

    Pretty simple, but at runtime, myStuffDao is null. I've used these persistence classes in a RESTful webapp, and they work fine. This is also in a webapp, but for some reason, Autowiring doesn't occur.

    Any tips on where to begin troubleshooting this would be great. I've been trying to get this to work for over a week now. I'm still a Spring amateur, so I'm hoping it's something obvious that I'm missing.
    Last edited by bm3719; Jan 18th, 2011, 01:34 PM.

  • #2
    Use the instance from the container, don't create a new instance yourself.

    That is probably what is going on.

    Comment


    • #3
      @Service annotation maybe missing

      Please check inf @Service annotation or @component annotation is missing at the top of your class before putting it in the scan

      Comment


      • #4
        Hi All.., i have a issue getting autowired bean in my service classes. Could anyone advice

        spring-config.xml -->
        <context:component-scan base-package="pershing.res.globalclearance" />
        <context:annotation-config/>


        java files --->

        @Autowired
        @Qualifier("GCLRRqstConvertor")
        private MessageConvertor GCLRRqstConvertor;

        @Autowired
        @Qualifier("GCLRRspConvertor")
        private MessageConvertor GCLRRspConvertor;

        public byte[] requestConversion {

        GCLRRqstConvertor.Convert(gCLRRADRequest); ----> NPE here

        }


        Comment


        • #5
          Error Stack :

          Error Stack :

          java.lang.NullPointerException
          at pershing.res.globalclearance.messages.GetOpenItems Converter.requestConversion(GetOpenItemsConverter. java:78)
          at pershing.res.globalclearance.service.gclrService.g etOpenItems(gclrService.java:53)
          at pershing.res.globalclearanceui.util.gclrUtil.getOp enItems(gclrUtil.java:22)
          at pershing.res.globalclearanceui.managedbeans.OpenIt emSummaryBean.getOpenItems(OpenItemSummaryBean.jav a:91)
          at pershing.res.globalclearanceui.managedbeans.Global ClearanceConfig.process(GlobalClearanceConfig.java :120)
          at pershing.res.globalclearanceui.managedbeans.Global ClearanceConfig.<init>(GlobalClearanceConfig.java: 36)
          at sun.reflect.NativeConstructorAccessorImpl.newInsta nce0(Native Method)
          at sun.reflect.NativeConstructorAccessorImpl.newInsta nce(NativeConstructorAccessorImpl.java:39)
          at sun.reflect.DelegatingConstructorAccessorImpl.newI nstance(DelegatingConstructorAccessorImpl.java:27)
          at java.lang.reflect.Constructor.newInstance(Construc tor.java:513)
          at java.lang.Class.newInstance0(Class.java:355)
          at java.lang.Class.newInstance(Class.java:308)
          at com.sun.faces.mgbean.BeanBuilder.newBeanInstance(B eanBuilder.java:188)
          at com.sun.faces.mgbean.BeanBuilder.build(BeanBuilder .java:102)
          at com.sun.faces.mgbean.BeanManager.createAndPush(Bea nManager.java:405)
          at com.sun.faces.mgbean.BeanManager.create(BeanManage r.java:267)
          at com.sun.faces.el.ManagedBeanELResolver.getValue(Ma nagedBeanELResolver.java:86)
          at javax.el.CompositeELResolver.getValue(CompositeELR esolver.java:67)
          at com.sun.faces.el.FacesCompositeELResolver.getValue (FacesCompositeELResolver.java:72)
          at org.apache.el.parser.AstIdentifier.getValue(AstIde ntifier.java:72)
          at org.apache.el.parser.AstValue.getValue(AstValue.ja va:161)
          at org.apache.el.ValueExpressionImpl.getValue(ValueEx pressionImpl.java:185)
          at com.sun.faces.facelets.el.TagValueExpression.getVa lue(TagValueExpression.java:102)
          at javax.faces.component.ComponentStateHelper.eval(Co mponentStateHelper.java:190)
          at javax.faces.component.ComponentStateHelper.eval(Co mponentStateHelper.java:178)
          at javax.faces.component.UIOutput.getValue(UIOutput.j ava:168)
          at com.sun.faces.renderkit.html_basic.HtmlBasicInputR enderer.getValue(HtmlBasicInputRenderer.java:205)
          at com.sun.faces.renderkit.html_basic.HtmlBasicRender er.getCurrentValue(HtmlBasicRenderer.java:338)
          at com.sun.faces.renderkit.html_basic.HtmlBasicRender er.encodeEnd(HtmlBasicRenderer.java:164)
          at javax.faces.component.UIComponentBase.encodeEnd(UI ComponentBase.java:878)
          at javax.faces.component.UIComponent.encodeAll(UIComp onent.java:1620)
          at javax.faces.component.UIComponent.encodeAll(UIComp onent.java:1616)
          at javax.faces.component.UIComponent.encodeAll(UIComp onent.java:1616)
          at com.sun.faces.application.view.FaceletViewHandling Strategy.renderView(FaceletViewHandlingStrategy.ja va:380)
          at com.sun.faces.application.view.MultiViewHandler.re nderView(MultiViewHandler.java:126)
          at com.sun.faces.lifecycle.RenderResponsePhase.execut e(RenderResponsePhase.java:127)
          at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:1 01)
          at com.sun.faces.lifecycle.LifecycleImpl.render(Lifec ycleImpl.java:139)
          at javax.faces.webapp.FacesServlet.service(FacesServl et.java:313)
          at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:305)
          at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:210)
          at org.apache.catalina.core.StandardWrapperValve.invo ke(StandardWrapperValve.java:222)
          at org.apache.catalina.core.StandardContextValve.invo ke(StandardContextValve.java:123)
          at org.apache.catalina.authenticator.AuthenticatorBas e.invoke(AuthenticatorBase.java:502)
          at org.apache.catalina.core.StandardHostValve.invoke( StandardHostValve.java:171)
          at org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:99)
          at org.apache.catalina.valves.AccessLogValve.invoke(A ccessLogValve.java:953)
          at org.apache.catalina.core.StandardEngineValve.invok e(StandardEngineValve.java:118)
          at org.apache.catalina.connector.CoyoteAdapter.servic e(CoyoteAdapter.java:408)
          at org.apache.coyote.http11.AbstractHttp11Processor.p rocess(AbstractHttp11Processor.java:1023)
          at org.apache.coyote.AbstractProtocol$AbstractConnect ionHandler.process(AbstractProtocol.java:589)
          at org.apache.tomcat.util.net.JIoEndpoint$SocketProce ssor.run(JIoEndpoint.java:312)
          at java.util.concurrent.ThreadPoolExecutor$Worker.run Task(ThreadPoolExecutor.java:886)
          at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:908)
          at java.lang.Thread.run(Thread.java:619)
          java.lang.NullPointerException
          at pershing.res.globalclearance.service.gclrService.g etOpenItems(gclrService.java:56)
          at pershing.res.globalclearanceui.util.gclrUtil.getOp enItems(gclrUtil.java:22)
          at pershing.res.globalclearanceui.managedbeans.OpenIt emSummaryBean.getOpenItems(OpenItemSummaryBean.jav a:91)
          at pershing.res.globalclearanceui.managedbeans.Global ClearanceConfig.process(GlobalClearanceConfig.java :120)
          at pershing.res.globalclearanceui.managedbeans.Global ClearanceConfig.<init>(GlobalClearanceConfig.java: 36)
          at sun.reflect.NativeConstructorAccessorImpl.newInsta nce0(Native Method)
          at sun.reflect.NativeConstructorAccessorImpl.newInsta nce(NativeConstructorAccessorImpl.java:39)
          at sun.reflect.DelegatingConstructorAccessorImpl.newI nstance(DelegatingConstructorAccessorImpl.java:27)
          at java.lang.reflect.Constructor.newInstance(Construc tor.java:513)
          at java.lang.Class.newInstance0(Class.java:355)
          at java.lang.Class.newInstance(Class.java:308)
          at com.sun.faces.mgbean.BeanBuilder.newBeanInstance(B eanBuilder.java:188)
          at com.sun.faces.mgbean.BeanBuilder.build(BeanBuilder .java:102)
          at com.sun.faces.mgbean.BeanManager.createAndPush(Bea nManager.java:405)
          at com.sun.faces.mgbean.BeanManager.create(BeanManage r.java:267)
          at com.sun.faces.el.ManagedBeanELResolver.getValue(Ma nagedBeanELResolver.java:86)
          at javax.el.CompositeELResolver.getValue(CompositeELR esolver.java:67)
          at com.sun.faces.el.FacesCompositeELResolver.getValue (FacesCompositeELResolver.java:72)
          at org.apache.el.parser.AstIdentifier.getValue(AstIde ntifier.java:72)
          at org.apache.el.parser.AstValue.getValue(AstValue.ja va:161)
          at org.apache.el.ValueExpressionImpl.getValue(ValueEx pressionImpl.java:185)
          at com.sun.faces.facelets.el.TagValueExpression.getVa lue(TagValueExpression.java:102)
          at javax.faces.component.ComponentStateHelper.eval(Co mponentStateHelper.java:190)
          at javax.faces.component.ComponentStateHelper.eval(Co mponentStateHelper.java:178)
          at javax.faces.component.UIOutput.getValue(UIOutput.j ava:168)
          at com.sun.faces.renderkit.html_basic.HtmlBasicInputR enderer.getValue(HtmlBasicInputRenderer.java:205)
          at com.sun.faces.renderkit.html_basic.HtmlBasicRender er.getCurrentValue(HtmlBasicRenderer.java:338)
          at com.sun.faces.renderkit.html_basic.HtmlBasicRender er.encodeEnd(HtmlBasicRenderer.java:164)
          at javax.faces.component.UIComponentBase.encodeEnd(UI ComponentBase.java:878)
          at javax.faces.component.UIComponent.encodeAll(UIComp onent.java:1620)
          at javax.faces.component.UIComponent.encodeAll(UIComp onent.java:1616)
          at javax.faces.component.UIComponent.encodeAll(UIComp onent.java:1616)
          at com.sun.faces.application.view.FaceletViewHandling Strategy.renderView(FaceletViewHandlingStrategy.ja va:380)
          at com.sun.faces.application.view.MultiViewHandler.re nderView(MultiViewHandler.java:126)
          at com.sun.faces.lifecycle.RenderResponsePhase.execut e(RenderResponsePhase.java:127)
          at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:1 01)
          at com.sun.faces.lifecycle.LifecycleImpl.render(Lifec ycleImpl.java:139)
          at javax.faces.webapp.FacesServlet.service(FacesServl et.java:313)
          at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:305)
          at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:210)
          at org.apache.catalina.core.StandardWrapperValve.invo ke(StandardWrapperValve.java:222)
          at org.apache.catalina.core.StandardContextValve.invo ke(StandardContextValve.java:123)
          at org.apache.catalina.authenticator.AuthenticatorBas e.invoke(AuthenticatorBase.java:502)
          at org.apache.catalina.core.StandardHostValve.invoke( StandardHostValve.java:171)
          at org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:99)
          at org.apache.catalina.valves.AccessLogValve.invoke(A ccessLogValve.java:953)
          at org.apache.catalina.core.StandardEngineValve.invok e(StandardEngineValve.java:118)
          at org.apache.catalina.connector.CoyoteAdapter.servic e(CoyoteAdapter.java:408)
          at org.apache.coyote.http11.AbstractHttp11Processor.p rocess(AbstractHttp11Processor.java:1023)
          at org.apache.coyote.AbstractProtocol$AbstractConnect ionHandler.process(AbstractProtocol.java:589)
          at org.apache.tomcat.util.net.JIoEndpoint$SocketProce ssor.run(JIoEndpoint.java:312)
          at java.util.concurrent.ThreadPoolExecutor$Worker.run Task(ThreadPoolExecutor.java:886)
          at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:908)
          at java.lang.Thread.run(Thread.java:619)

          Comment


          • #6
            Autowiring beans will only work on instances provided by the container, not on instance manually created by you.
            Last edited by _0R10N; Feb 20th, 2014, 12:48 PM.

            Comment

            Working...
            X