3
votes

I'm pretty new to the whole idea of injecting beans, so speak slowly. :)

I've got a class that injects a bean, but when the property is accessed, the property is null and I get a null-pointer exception.

From /project-TRUNK/war-module/src/main/webapp/WEB-INF/spring-config/spring-bean.xml

 <bean id="linkCheck"
  class="com.dogwatch.util.LinkCheck">
  <property name="linkDao" ref="jdbcLinkDao" />
 </bean>

From /project-TRUNK/war-module/src/main/webapp/WEB-INF/spring-config/spring-dao.xml

 <bean id="jdbcLinkDao" class="com.dogwatch.util.jdbcLinkDao">
  <property name="dataSource" ref="dataSource" />
  <property name="linkJdbcDataTypesSupport" ref="linkJdbcDataTypesSupport"/>
 </bean>

The DAO bean is known to be good and works in several other classes.

package com.dogwatch.util;
public class LinkCheck {

 private LinkDAO linkDao;


 public LinkDAO getLinkDao() {
  return linkDao;
 }

 public void setLinkDao(LinkDAO linkDao) {
  this. linkDao = linkDao;
 }
}

I've been comparing it to other classes that use the same DAO bean and I can't find any differences.

I do see the bean getting defined in:

INFO [2010-01-15 01:10:05,838] [main] [XmlBeanDefinitionReader] [XmlBeanDefinitionReader.java:323] - Loading XML bean definitions from URL [file:war-module/src/main/webapp/WEB-INF/spring-config/spring-dao.xml] INFO [2010-01-15 01:10:05,858] [main] [XmlBeanDefinitionReader] [XmlBeanDefinitionReader.java:323] - Loading XML bean definitions from URL [file:war-module/src/main/webapp/WEB-INF/spring-config/spring-bean.xml] INFO [2010-01-15 01:10:06,597] [main] [DefaultListableBeanFactory] [DefaultListableBeanFactory.java:414] - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@3c01d5a0: defining beans [(snip) jdbcLinkDao, linkCheck(snip)businessLoggingAspect,org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#1]; root of factory hierarchy

Does anyone have any suggestions on how to go about troubleshooting bean injection?

2
Typo leading to first two answers fixed.wadesworld
We're still missing some information, such as how does the code that uses the linkCheck bean get it? Is it also injected? Do you retrieve it from the Spring context? I'm assuming you're not doing new LinkCheck(), but since you're new to Spring, it's a sensible starting question.SteveD
@Wade: Fix your spring-dao.xml too. Thanks. And yes, answer Stevendick.Adeel Ansari
@StevenDick, doh...told you I was new...I did still have a new in there. I changed it to use getBean() and it now works correctly - thanks. If you want to convert your comment to an answer, I'll accept it.wadesworld
For me I had a similar problem but the problem turned out that I had duplicate versions of a jar in my classpath, so it was loading my xml definitions from "an old jar" which caused confusion. Odd.rogerdpack

2 Answers

8
votes

I'm expanding my comment from the OP's question since it appears to have helped...

We're still missing some information, such as how does the code that uses the linkCheck bean get it? Is it also injected? Do you retrieve it from the Spring context? I'm assuming you're not doing new LinkCheck(), but since you're new to Spring, it's a sensible starting question.

Spring can only inject the beans if it controls the creation of the beans: if you do new MyClass() then the Spring container doesn't know about the object and can't do the injection.

1
votes

There is a potential problem in that the type of 'jdbcLinkDao' is 'jdbcLinkDao' but the argument type of the setter is LinkDAO. (If 'jdbcLinkDao' is a subtype of 'LinkDAO' that is probably OK ... but FIX THE CLASS NAME!)

If that's not the problem I suggest that you:

  • set the log level to DEBUG,

  • if that doesn't show anything, add a trace print to LinkCheck.setLinkDAO() to see what value is being set ... and when it is being set.