hibernate - Spring and null pointer exception -


i'm trying this:

@controller     public class homecontroller {          @autowired         private templatesservice templatesservice;              public final string test = templatesservice.geturi(); 

the geturi:

@suppresswarnings("unchecked")     public string geturi() {          return (string) sessionfactory.getcurrentsession()                 .createquery("select uri templates state=1")                 .uniqueresult();     } 

it work correctly, if i'll declare test in method. i'm having this:

org.springframework.beans.factory.beancreationexception: error creating bean name 'homecontroller' defined in file [c:\users\anton\springsource\vfabric-tc-server-developer-2.8.2.release\base-instance\wtpwebapps\blog\web-inf\classes\net\babobka\blog\controller\homecontroller.class]: instantiation of bean failed; nested exception org.springframework.beans.beaninstantiationexception: not instantiate bean class [net.babobka.blog.controller.homecontroller]: constructor threw exception; nested exception java.lang.nullpointerexception     @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.instantiatebean(abstractautowirecapablebeanfactory.java:997)     @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbeaninstance(abstractautowirecapablebeanfactory.java:943)     @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(abstractautowirecapablebeanfactory.java:485)     @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbean(abstractautowirecapablebeanfactory.java:456)     @ org.springframework.beans.factory.support.abstractbeanfactory$1.getobject(abstractbeanfactory.java:294)     @ org.springframework.beans.factory.support.defaultsingletonbeanregistry.getsingleton(defaultsingletonbeanregistry.java:225)     @ org.springframework.beans.factory.support.abstractbeanfactory.dogetbean(abstractbeanfactory.java:291)     @ org.springframework.beans.factory.support.abstractbeanfactory.getbean(abstractbeanfactory.java:193)     @ org.springframework.beans.factory.support.defaultlistablebeanfactory.preinstantiatesingletons(defaultlistablebeanfactory.java:585)     @ org.springframework.context.support.abstractapplicationcontext.finishbeanfactoryinitialization(abstractapplicationcontext.java:913)     @ org.springframework.context.support.abstractapplicationcontext.refresh(abstractapplicationcontext.java:464)     @ org.springframework.web.context.contextloader.configureandrefreshwebapplicationcontext(contextloader.java:385)     @ org.springframework.web.context.contextloader.initwebapplicationcontext(contextloader.java:284)     @ org.springframework.web.context.contextloaderlistener.contextinitialized(contextloaderlistener.java:111)  

my root-context(i don't think it's helpful):

    <context:annotation-config />       <context:component-scan base-package="net.babobka.blog" />     <mvc:resources mapping="/resources/**" location="/resources/" />     <cache:annotation-driven />      <bean id="cachemanager" class="org.springframework.cache.support.simplecachemanager">         <property name="caches">             <set>                 <bean                     class="org.springframework.cache.concurrent.concurrentmapcachefactorybean"                     p:name="template" />                 <bean                     class="org.springframework.cache.concurrent.concurrentmapcachefactorybean"                     p:name="headhunter" />              </set>         </property>     </bean>      <bean id="converter" class="net.babobka.blog.headhunter.converter" />       <bean id="propertyconfigurer"         class="org.springframework.beans.factory.config.propertyplaceholderconfigurer"         p:location="/web-inf/db/jdbc.properties" />      <bean id="datasource" class="org.apache.commons.dbcp.basicdatasource"         destroy-method="close" p:driverclassname="${jdbc.driverclassname}"         p:url="${jdbc.databaseurl}" p:username="${jdbc.username}" p:password="${jdbc.password}" />     <bean id="sessionfactory"         class="org.springframework.orm.hibernate3.localsessionfactorybean">         <property name="datasource" ref="datasource" />         <property name="configlocation">             <value>/web-inf/db/hibernate.cfg.xml</value>         </property>         <property name="configurationclass">             <value>org.hibernate.cfg.annotationconfiguration</value>         </property>         <property name="hibernateproperties">             <props>                 <prop key="hibernate.dialect">${jdbc.dialect}</prop>                 <prop key="hibernate.show_sql">${jdbc.show_sql}</prop>                 <prop key="hibernate.connection.useunicode">true</prop>                 <prop key="hibernate.connection.characterencoding">utf-8</prop>                 <prop key="hibernate.connection.charset">utf-8</prop>             </props>         </property>     </bean>         <bean id="transactionmanager"         class="org.springframework.orm.hibernate3.hibernatetransactionmanager">         <property name="sessionfactory" ref="sessionfactory" />     </bean>     <mvc:annotation-driven />     <tx:annotation-driven />     <task:annotation-driven />        <bean id="backupper" class="net.babobka.blog.backupper.backupper"></bean>      <bean id="olddataremoval" class="net.babobka.blog.termination.olddataremoval"></bean>      <bean id="headhunterimport" class="net.babobka.blog.headhunter.headhunterimport"></bean>      <bean id="urlforwardcontroller"         class="org.springframework.web.servlet.mvc.urlfilenameviewcontroller" />      <bean id="tilesconfigurer"         class="org.springframework.web.servlet.view.tiles2.tilesconfigurer">         <property name="definitions">             <list>                 <value>/web-inf/tiles.xml</value>             </list>         </property>     </bean>      <bean id="viewresolver"         class="org.springframework.web.servlet.view.urlbasedviewresolver">         <property name="viewclass"             value="org.springframework.web.servlet.view.tiles2.tilesview" />     </bean>      </beans> 

what's wrong?

it's not working because templatesservice not yet injected when reference set test. fields injection work after instantiation of object. need solve use @postconstruct set test.

@controller     public class homecontroller {      @autowired     private templatesservice templatesservice;          public string test;      @postconstruct     public void init() {        this.test = templatesservice.geturi();     } 

if still want use final, can use injection constructor , set test then.

  @controller       public class homecontroller {      private templatesservice templatesservice;          public final string test;      @autowired     public homecontroller(templatesservice templatesservice) {         this.templatesservice = templatesservice;         this.test = templatesservice.geturi();     } 

Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

c++ - Clear the memory after returning a vector in a function -

erlang - Saving a digraph to mnesia is hindered because of its side-effects -