PDA

View Full Version : Why no body answer this question? NullPointerException when calling method on spring


oawad79
Apr 3rd, 2008, 10:19 AM
Hello All,
i am new to spring and I would appreciate any help in this?

i have the following spring bean class

public class HouseHoldingDBFacade implements IHouseHoldingDBDacade {
private IUsersDAO usersDao;

public HouseHoldingDBFacade() {

}

public void setUsersDao(IUsersDAO usersDao) {
System.out.println("usersDao null? " + (usersDao == null));
this.usersDao = usersDao;
}



public IUsersDAO getUsersDao() {
return usersDao;
}

public UserBean isValidUserNamePassword(UserBean userBean) {
System.out.println("usersDAO is null? " + (usersDao == null));
return getUsersDao().isValidUserNamePassword(userBean);
}

}

and in applicationContext.xml i have the following

<bean id="derbyHHDataSource" class="org.springframework.jndi.JndiObjectFactoryBean" scope="singleton">
<property name="jndiName" value="java:comp/env/jdbc/hhDb" />
<property name="resourceRef" value="false"/>
</bean>

<!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) -->
<bean id="derbyUsersDataProvider" class="com.springjsf.persistence.dao.derbyimpl.DerbyUsers DAO">
<property name="dataSource">
<ref local="derbyHHDataSource"/>
</property>


</bean>

<bean id="usersDao" class="com.springjsf.persistence.HouseHoldingDBFacade">
<property name="usersDao">
<ref bean="derbyUsersDataProvider"/>
</property>
</bean>



when I deploy the application I get in the first method setUsersDao System.out prints that usersDao != null and when I run the application on the isValidUserNamePassword i get that usersDao = null ? but why???????

smrutimo
Apr 3rd, 2008, 11:11 AM
Are you creating a new instance of HouseHoldingDBFacade from whereever u are calling it ? Make sure that your are using the same instance that you injected and not a new instance.

oawad79
Apr 3rd, 2008, 11:32 AM
thanks man, that was the issue.

I was everytime doing constructor injection with a new instance

I appreciate your help!