hibernate - Null Pointer with Spring @Autowired -


i new spring & hibernate , have spent couple of weeks trying grips it. of experience oracle development departure me. hibernate working spring giving me headache.

i tried following tutorial using oracle db , address table.

http://www.codejava.net/frameworks/spring/spring-4-and-hibernate-4-integration-tutorial-part-1-xml-configuration

the web page wasn't working tried test case on homecontroller.java didn't work believe have narrowed down null pointer in addressdao , wrote test case try pin down.

java.lang.nullpointerexception: null springexp.unittest.addressdaotest.testlistaddr(addressdaotest.java:29)

package springexp.unittest;  import bussys.model.address; import springexp.dao.addressdao; import java.util.list; import static org.junit.assert.*; import org.junit.test; import org.springframework.beans.factory.annotation.autowired; import org.springframework.web.bind.annotation.requestmapping;  public class addressdaotest {   @autowired   private addressdao addressdao;    @requestmapping(value="/")   /**    * @see springexp.dao.addressdao#listaddr()    */   @test  public void testlistaddr()  {     list<address> listaddresses = addressdao.listaddr();  << error thrown here     system.out.println(listaddresses.get(1).getfirstname());      fail("unimplemented");   } } 

i have spent long time reading stackoverflow , others try fix null pointer issue there seems lots of ways of writing , has changed in various versions.

in servlet-context.xml have this:

 <!-- hibernate session factory -->   <bean id="sessionfactory"       class="org.springframework.orm.hibernate4.localsessionfactorybean">       <property name="datasource" ref="datasource" />       <property name="annotatedclasses">         <list>           <value>springexp.dao.addressdao</value>         </list>       </property>   </bean>    <tx:annotation-driven />   <bean id="transactionmanager"      class="org.springframework.orm.hibernate4.hibernatetransactionmanager">       <property name="sessionfactory" ref="sessionfactory" />   </bean>    <bean id="addressdao" class="springexp.dao.addressdaoimpl">       <constructor-arg>           <ref bean="sessionfactory" />       </constructor-arg>   </bean>    <!-- added based on 1 suggestion -->   <bean id="addressdaotest" class="springexp.unittest.addressdaotest">       <constructor-arg>           <ref bean="sessionfactory" />       </constructor-arg>   </bean> 

addressdaoimpl.java

 package springexp.dao;     import bussys.model.address;     import java.util.list;      import org.hibernate.criteria;     import org.hibernate.sessionfactory;     import org.springframework.transaction.annotation.transactional;      public class addressdaoimpl       implements addressdao     {       private sessionfactory sessionfactory;        public addressdaoimpl(sessionfactory sessionfactory)        {         this.sessionfactory = sessionfactory;       }        @override       @transactional       public list<address> listaddr()       {         @suppresswarnings("unchecked")          list<address> listaddress = (list<address>)              sessionfactory.getcurrentsession().createcriteria(address.class).setresulttransformer(criteria.distinct_root_entity).list();         return listaddress;       }     } 

i hope enough give clue mistake(s).


Comments