Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Spring Injection - NullPointerException

 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys,

I have a couple of applicationContext-xxx-xxx.xml files. I have a master applicationContext.xml file where I import all the other files. Now when I start my application every bean gets initialized. When I run my application, I get a NullPointer as one of the bean that I define in my applicationContext-xxx-1.xml is being referenced in applicationContext-xxx-2.xml. I was under the impression that this should not be a problem as during runtime they all know about each other. Any ideas what might be wrong here that leads to this exception.
 
Jayesh A Lalwani
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you writing the beans? Are you using XML based configuration? Are you Autowiring? Please TellTheDetails
 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As was stated you need to give us more details:
1. stack trace
2. The configuration for the bean causing the issue
3. Your web.xml so we can see how you are loading your application contexts files.


That said I assume you have beans with dependencies on other beans in different xml files. Are you using the ref attribute for this? If you are and visibility to that bean was an issue I would be expecting to see a NoSuchBeanDefintionException not a null pointer.
You say that every bean gets initialized. Have you validated this? If not its possible you are loading one context file only and if you have not imported the others they are not getting initialized. Lastly if you have a dependency in one bean on another and you are not using the ref attribute (for example if the bean is dependent on the other from something happening in a static initializer block) then you will need to use the dependsOn attribute to notify Spring explicitly that there is a dependency.

Lastly is this error occur on startup? If not then ignore everything I just stated and first make sure that you are actually using a Spring managed bean. Dependency Injection is only done on Spring managed beans.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic