Two Laptop Bag
The moose likes Spring and the fly likes Spring Injection - NullPointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Spring Injection - NullPointerException" Watch "Spring Injection - NullPointerException" New topic

Spring Injection - NullPointerException

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 10033


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.

SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Jayesh A Lalwani
Saloon Keeper

Joined: Jan 17, 2008
Posts: 2748

How are you writing the beans? Are you using XML based configuration? Are you Autowiring? Please TellTheDetails
Bill Gorder

Joined: Mar 07, 2010
Posts: 1682

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.

[How To Ask Questions][Read before you PM me]
I agree. Here's the link:
subject: Spring Injection - NullPointerException
It's not a secret anymore!