0

I get a null pointer exception when trying to inject an object. Here is my code:

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml
</param-value>
 </context-param>'

ApplicationContext.XML

<bean id="accessDao" 
 Class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" 
  autowire-candidate="true">
  <property name="transactionManager" ref="txManager" />   
  <property name="target" ref="accessDaoTarget" />   
   <property name="transactionAttributes">   
   <props>   
  <prop key="*">PROPAGATION_REQUIRED</prop>   
   </props>   
  </property>   

</bean>   '

CommonBean

import com.domain.dao.IDao;
@Named
public class CommonBean implements Serializable{

/**
 * 
 */

private static final long serialVersionUID = 1L;
@Inject
private IDao accessDao;


public IDao getAccessDao()

      {
        return accessDao;
      }

 public void setAccessDao(IDao accessDao)
  {
    this.accessDao = accessDao;
  }

}
4
  • Do you have "context:component-scan" defined in your applicationContext.xml context file. Commented Feb 22, 2013 at 6:32
  • s..Here it is <context:component-scan base-package="com.myjsf.appl.CommonBean" /> Commented Feb 22, 2013 at 6:38
  • Have you tried with @Autowired instead of @Inject? Commented Feb 22, 2013 at 8:06
  • Hi..I tried..but no use.. Commented Feb 22, 2013 at 8:42

2 Answers 2

0

The reason i suppose is because the component scan should include all the files that annotated by Spring. So for this to work , broaden the scope of packages to scan.

change from

<context:component-scan base-package="com.myjsf.appl.CommonBean" />

to

 <context:component-scan base-package="com.domain,com.myjsf" />
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your immediately reply Mr.sudhakar..I tried it but still getting the same exception
Hi mohan he s referring two different packages seperated by comma.
Can you make sure you add all packages that use spring annotation to the <context:component-scan> tag, also can you post your applicationContext.xml and your package structure
-1

I think the reason is because you are refering to an "accessDao" bean which implements a IDAO interface. The bean accessDao declared on applicationContext.xml is of type org.springframework.transaction.interceptor.TransactionProxyFactoryBean which implements the BeanFactoryAware interface and NOT the IDAO interface.

As so spring will not recognize the bean you are trying to inject (IDAO accessDAO) and your property will not be initialized.

1 Comment

Wrong. That bean will implement interfaces defined by the target specified.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.