Announcement Announcement Module
Collapse
No announcement yet.
NullPointerException when use HibernateTemplate! Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • NullPointerException when use HibernateTemplate!

    Hi everyone:

    I use HibernateDaoSupport in spring web app. When I run the code ,Tomcat report NullPointerException in line:"this.getHibernateTemplate()"

    My config file is:
    Code:
    <bean id="MyDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
            <!-- results in a setDriverClassName&#40;String&#41; call -->
            <property name="driverClassName">
                <value>com.mysql.jdbc.Driver</value>
            </property>
            <property name="url">
                <value>jdbc&#58;mysql&#58;//127.0.0.1&#58;3306/spring?useUnicode=true&amp;characterEncoding=gb2312</value>
            </property>
            <property name="username">
                <value>root</value>
            </property>
    		<property name="password">
                <value>root</value>
            </property>
    		<property name="defaultAutoCommit"> 
                <value>false</value> 
            </property>
        </bean>
    	<!--
    <bean id="MyJNDIDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
      <property name="jndiName">
        <value>jdbc/mysql</value>
      </property>
    </bean>
    -->
    <bean id="MySessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
       <property name="mappingResources">
          <list>
            <value>lyo/test/bean/Parent.hbm.xml</value>
            <value>lyo/test/bean/Son.hbm.xml</value>
            <value>lyo/test/bean/C.hbm.xml</value>
    
          </list>
       </property>
       <property name="hibernateProperties">
          <props>
            <prop key="hibernate.dialect">net.sf.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.query.substitutions">true=1 false=0</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.use_outer_join">false</prop>
          </props>
       </property>
       <property name="dataSource"><ref bean="MyDataSource"/></property>
    </bean>
    <bean id="sondao" class="lyo.test.dao.SonDAO">
       <property name="sessionFactory">
          <ref bean="MySessionFactory"/>
       </property>
    </bean>
    <bean id="parentdao" class="lyo.test.dao.ParentDAO">
       <property name="sessionFactory">
          <ref bean="MySessionFactory"/>
       </property>
    
    </bean>
    <bean id="userServlet" class="lyo.test.servlet.UserActionServlet">
       <property name="pdao">
          <ref bean="parentdao"/>
       </property>
    
    </bean>
    My ParentDAO is:
    Code:
    public class ParentDAO extends HibernateDaoSupport&#123;
    	
    	private static Log log=LogFactory.getLog&#40;ParentDAO.class&#41;;
    	
    	public Long saveParent&#40;Object o&#41;&#123;
    		Object obj=null;
    	
    obj=this.getHibernateTemplate&#40;&#41;.save&#40;o&#41;; <--Exception
    		log.info&#40;"Saved id&#58; "+obj&#41;;
    	
    		return &#40;Long&#41;obj;
    	&#125;
    Servlet code:

    Code:
    Parent p=new Parent&#40;&#41;;
    	     p.setName&#40;parentname&#41;;
    	     ParentDAO pdao=new ParentDAO&#40;&#41;;
    	     Long id=pdao.saveParent&#40;p&#41;;
    Exception is:
    Code:
    14&#58;18&#58;01,845  INFO DispatcherServlet&#58;103 - Servlet 'springmodal' configured succ
    essfully
    java.lang.NullPointerException
            at lyo.test.dao.ParentDAO.saveParent&#40;ParentDAO.java&#58;23&#41;
            at lyo.test.servlet.UserActionServlet.doPost&#40;UserActionServlet.java&#58;68&#41;
            at javax.servlet.http.HttpServlet.service&#40;HttpServlet.java&#58;709&#41;
            at javax.servlet.http.HttpServlet.service&#40;HttpServlet.java&#58;802&#41;
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter&#40;Appl
    icationFilterChain.java&#58;237&#41;
            at org.apache.catalina.core.ApplicationFilterChain.doFilter&#40;ApplicationF
    ilterChain.java&#58;157&#41;
            at lyo.test.util.TestHiberFilter.doFilter&#40;TestHiberFilter.java&#58;41&#41;
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter&#40;Appl
    icationFilterChain.java&#58;186&#41;
            at org.apache.catalina.core.ApplicationFilterChain.doFilter&#40;ApplicationF
    ilterChain.java&#58;157&#41;
            at org.apache.catalina.core.StandardWrapperValve.invoke&#40;StandardWrapperV
    alve.java&#58;214&#41;
            at org.apache.catalina.core.StandardValveContext.invokeNext&#40;StandardValv
    eContext.java&#58;104&#41;
            at org.apache.catalina.core.StandardPipeline.invoke&#40;StandardPipeline.jav
    a&#58;520&#41;
            at org.apache.catalina.core.StandardContextValve.invokeInternal&#40;Standard
    ContextValve.java&#58;198&#41;
            at org.apache.catalina.core.StandardContextValve.invoke&#40;StandardContextV
    alve.java&#58;152&#41;
            at org.apache.catalina.core.StandardValveContext.invokeNext&#40;StandardValv
    eContext.java&#58;104&#41;
            at org.apache.catalina.core.StandardPipeline.invoke&#40;StandardPipeline.jav
    a&#58;520&#41;
            at org.apache.catalina.core.StandardHostValve.invoke&#40;StandardHostValve.j
    ava&#58;137&#41;
            at org.apache.catalina.core.StandardValveContext.invokeNext&#40;StandardValv
    eContext.java&#58;104&#41;
            at org.apache.catalina.valves.ErrorReportValve.invoke&#40;ErrorReportValve.j
    ava&#58;118&#41;
            at org.apache.catalina.core.StandardValveContext.invokeNext&#40;StandardValv
    eContext.java&#58;102&#41;
            at org.apache.catalina.core.StandardPipeline.invoke&#40;StandardPipeline.jav
    a&#58;520&#41;
            at org.apache.catalina.core.StandardEngineValve.invoke&#40;StandardEngineVal
    ve.java&#58;109&#41;
            at org.apache.catalina.core.StandardValveContext.invokeNext&#40;StandardValv
    eContext.java&#58;104&#41;
            at org.apache.catalina.core.StandardPipeline.invoke&#40;StandardPipeline.jav
    a&#58;520&#41;
            at org.apache.catalina.core.ContainerBase.invoke&#40;ContainerBase.java&#58;929&#41;
    
            at org.apache.coyote.tomcat5.CoyoteAdapter.service&#40;CoyoteAdapter.java&#58;16
    0&#41;
            at org.apache.coyote.http11.Http11Processor.process&#40;Http11Processor.java
    &#58;799&#41;
            at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.proce
    ssConnection&#40;Http11Protocol.java&#58;705&#41;
            at org.apache.tomcat.util.net.TcpWorkerThread.runIt&#40;PoolTcpEndpoint.java
    &#58;577&#41;
            at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run&#40;ThreadP
    ool.java&#58;684&#41;
            at java.lang.Thread.run&#40;Thread.java&#58;534&#41;
    Why I can't get HibernateTemplate? what's wrong with me? Help!

  • #2
    Re: NullPointerException when use HibernateTemplate!

    Originally posted by liren
    Hi everyone:

    I use HibernateDaoSupport in spring web app. When I run the code ,Tomcat report NullPointerException in line:"this.getHibernateTemplate()"

    My config file is:
    Code:
    <bean id="MyDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
            <!-- results in a setDriverClassName&#40;String&#41; call -->
            <property name="driverClassName">
                <value>com.mysql.jdbc.Driver</value>
            </property>
            <property name="url">
                <value>jdbc&#58;mysql&#58;//127.0.0.1&#58;3306/spring?useUnicode=true&amp;characterEncoding=gb2312</value>
            </property>
            <property name="username">
                <value>root</value>
            </property>
    		<property name="password">
                <value>root</value>
            </property>
    		<property name="defaultAutoCommit"> 
                <value>false</value> 
            </property>
        </bean>
    	<!--
    <bean id="MyJNDIDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
      <property name="jndiName">
        <value>jdbc/mysql</value>
      </property>
    </bean>
    -->
    <bean id="MySessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
       <property name="mappingResources">
          <list>
            <value>lyo/test/bean/Parent.hbm.xml</value>
            <value>lyo/test/bean/Son.hbm.xml</value>
            <value>lyo/test/bean/C.hbm.xml</value>
    
          </list>
       </property>
       <property name="hibernateProperties">
          <props>
            <prop key="hibernate.dialect">net.sf.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.query.substitutions">true=1 false=0</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.use_outer_join">false</prop>
          </props>
       </property>
       <property name="dataSource"><ref bean="MyDataSource"/></property>
    </bean>
    <bean id="sondao" class="lyo.test.dao.SonDAO">
       <property name="sessionFactory">
          <ref bean="MySessionFactory"/>
       </property>
    </bean>
    <bean id="parentdao" class="lyo.test.dao.ParentDAO">
       <property name="sessionFactory">
          <ref bean="MySessionFactory"/>
       </property>
    
    </bean>
    <bean id="userServlet" class="lyo.test.servlet.UserActionServlet">
       <property name="pdao">
          <ref bean="parentdao"/>
       </property>
    
    </bean>
    My ParentDAO is:
    Code:
    public class ParentDAO extends HibernateDaoSupport&#123;
    	
    	private static Log log=LogFactory.getLog&#40;ParentDAO.class&#41;;
    	
    	public Long saveParent&#40;Object o&#41;&#123;
    		Object obj=null;
    	
    obj=this.getHibernateTemplate&#40;&#41;.save&#40;o&#41;; <--Exception
    		log.info&#40;"Saved id&#58; "+obj&#41;;
    	
    		return &#40;Long&#41;obj;
    	&#125;
    Servlet code:

    Code:
    Parent p=new Parent&#40;&#41;;
    	     p.setName&#40;parentname&#41;;
    	     ParentDAO pdao=new ParentDAO&#40;&#41;;
    	     Long id=pdao.saveParent&#40;p&#41;;
    Exception is:
    Code:
    14&#58;18&#58;01,845  INFO DispatcherServlet&#58;103 - Servlet 'springmodal' configured succ
    essfully
    java.lang.NullPointerException
            at lyo.test.dao.ParentDAO.saveParent&#40;ParentDAO.java&#58;23&#41;
            at lyo.test.servlet.UserActionServlet.doPost&#40;UserActionServlet.java&#58;68&#41;
            at javax.servlet.http.HttpServlet.service&#40;HttpServlet.java&#58;709&#41;
            at javax.servlet.http.HttpServlet.service&#40;HttpServlet.java&#58;802&#41;
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter&#40;Appl
    icationFilterChain.java&#58;237&#41;
            at org.apache.catalina.core.ApplicationFilterChain.doFilter&#40;ApplicationF
    ilterChain.java&#58;157&#41;
            at lyo.test.util.TestHiberFilter.doFilter&#40;TestHiberFilter.java&#58;41&#41;
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter&#40;Appl
    icationFilterChain.java&#58;186&#41;
            at org.apache.catalina.core.ApplicationFilterChain.doFilter&#40;ApplicationF
    ilterChain.java&#58;157&#41;
            at org.apache.catalina.core.StandardWrapperValve.invoke&#40;StandardWrapperV
    alve.java&#58;214&#41;
            at org.apache.catalina.core.StandardValveContext.invokeNext&#40;StandardValv
    eContext.java&#58;104&#41;
            at org.apache.catalina.core.StandardPipeline.invoke&#40;StandardPipeline.jav
    a&#58;520&#41;
            at org.apache.catalina.core.StandardContextValve.invokeInternal&#40;Standard
    ContextValve.java&#58;198&#41;
            at org.apache.catalina.core.StandardContextValve.invoke&#40;StandardContextV
    alve.java&#58;152&#41;
            at org.apache.catalina.core.StandardValveContext.invokeNext&#40;StandardValv
    eContext.java&#58;104&#41;
            at org.apache.catalina.core.StandardPipeline.invoke&#40;StandardPipeline.jav
    a&#58;520&#41;
            at org.apache.catalina.core.StandardHostValve.invoke&#40;StandardHostValve.j
    ava&#58;137&#41;
            at org.apache.catalina.core.StandardValveContext.invokeNext&#40;StandardValv
    eContext.java&#58;104&#41;
            at org.apache.catalina.valves.ErrorReportValve.invoke&#40;ErrorReportValve.j
    ava&#58;118&#41;
            at org.apache.catalina.core.StandardValveContext.invokeNext&#40;StandardValv
    eContext.java&#58;102&#41;
            at org.apache.catalina.core.StandardPipeline.invoke&#40;StandardPipeline.jav
    a&#58;520&#41;
            at org.apache.catalina.core.StandardEngineValve.invoke&#40;StandardEngineVal
    ve.java&#58;109&#41;
            at org.apache.catalina.core.StandardValveContext.invokeNext&#40;StandardValv
    eContext.java&#58;104&#41;
            at org.apache.catalina.core.StandardPipeline.invoke&#40;StandardPipeline.jav
    a&#58;520&#41;
            at org.apache.catalina.core.ContainerBase.invoke&#40;ContainerBase.java&#58;929&#41;
    
            at org.apache.coyote.tomcat5.CoyoteAdapter.service&#40;CoyoteAdapter.java&#58;16
    0&#41;
            at org.apache.coyote.http11.Http11Processor.process&#40;Http11Processor.java
    &#58;799&#41;
            at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.proce
    ssConnection&#40;Http11Protocol.java&#58;705&#41;
            at org.apache.tomcat.util.net.TcpWorkerThread.runIt&#40;PoolTcpEndpoint.java
    &#58;577&#41;
            at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run&#40;ThreadP
    ool.java&#58;684&#41;
            at java.lang.Thread.run&#40;Thread.java&#58;534&#41;
    Why I can't get HibernateTemplate? what's wrong with me? Help!
    One thing to try to set spring to debug in your log4j properties file to get some more info on where spring is at when the exceptions happen. Mostly i found these kinda issues to be configuration related..

    Greetz
    Leo

    Comment


    • #3
      Re: NullPointerException when use HibernateTemplate!

      Originally posted by liren
      Code:
      ParentDAO pdao=new ParentDAO&#40;&#41;;
      ParentDAO is Spring managed, you must get it from ApplicationContext.
      ParentDAO pdao = appContext.get("myParentDAO");

      Comment


      • #4
        Re: NullPointerException when use HibernateTemplate!

        Originally posted by maramonar
        Originally posted by liren
        Code:
        ParentDAO pdao=new ParentDAO&#40;&#41;;
        ParentDAO is Spring managed, you must get it from ApplicationContext.
        ParentDAO pdao = appContext.get("myParentDAO");
        I think I should post my servlet code here.Maybe the problem is in my Servlet.

        Code:
         public class UserActionServlet extends HttpServlet&#123;
                 public void doGet&#40;............................&#41;&#123;...............&#125;
                 public void doPost&#40;............................&#41;&#123;
                    .................................................
                 Parent p=new Parent&#40;&#41;; 
                p.setName&#40;parentname&#41;; 
                ParentDAO pdao=new ParentDAO&#40;&#41;; 
                Long id=pdao.saveParent&#40;p&#41;;
                 ........................................................
                &#125;
        After saw your post. I change my servlet code to the following ,But it can't work too. ;-(

        Code:
         public class UserActionServlet extends HttpServlet&#123;
        
                private ParentDAO  pdao;
        
               public void setPdao&#40;ParentDAO pdao&#41;&#123;
                        this.pdao=pdao;
                &#125;
               public ParentDAO getPdao&#40;&#41;&#123;
                       return this.pdao;
               &#125;
        
                 public void doGet&#40;............................&#41;&#123;...............&#125;
                 public void doPost&#40;............................&#41;&#123;
                    .................................................
                 Parent p=new Parent&#40;&#41;; 
                p.setName&#40;parentname&#41;; 
               &#91;b&#93; ParentDAO pdao=this.getPdao&#40;&#41;;&#91;/b&#93;
                Long id=pdao.saveParent&#40;p&#41;;
                 ........................................................
                &#125;
        In my config file :
        Code:
        <bean id="userServlet" class="lyo.test.servlet.UserActionServlet"> 
           <property name="pdao"> 
              <ref bean="parentdao"/> 
           </property> 
        
        </bean>
        But it remain throws NullPointerException! why ? Is there any problem in my code? Need my servlet have to extends Controller Servlet? [/b]

        Comment


        • #5


          Hi, I change my code to :
          Code:
          String path=this.getServletConfig&#40;&#41;.getServletContext&#40;&#41;.getRealPath&#40;"."&#41;;
          	     System.out.println&#40;"Get path&#58; "+path&#41;;
          	     ApplicationContext spring=
          			new FileSystemXmlApplicationContext&#40;path+"/WEB-INF/springmodal-servlet.xml"&#41;;
          	      ParentDAO pdao= &#40;ParentDAO&#41;spring.getBean&#40;"parentdao"&#41;;
          	    
          	     Parent p=new Parent&#40;&#41;;
          	     p.setName&#40;parentname&#41;;
          	     Long id=pdao.saveParent&#40;p&#41;;
          It don' t throw Nullpointer Exception and console output this info:
          Code:
          Hibernate&#58; insert into PARENT &#40;name&#41; values &#40;?&#41;
          10&#58;34&#58;14,454  INFO ParentDAO&#58;17 - Saved id&#58; 37
          10&#58;34&#58;14,464  INFO UserActionServlet&#58;80 - get id&#58; 37
          But there is not any records be inserted into database! my saveParent method is:
          Code:
          public Long saveParent&#40;Object o&#41;&#123;
          		Object obj=null;
          	
          		obj=this.getSession&#40;&#41;.save&#40;o&#41;;
          		log.info&#40;"Saved id&#58; "+obj&#41;;
          	
          		return &#40;Long&#41;obj;
          	&#125;
          What's wrong with me? Need I have to commit a transaction in spring?

          Comment


          • #6
            What's wrong with me? Need I have to commit a transaction in spring?
            You need to configure Transaction Management around your dao methods. For more information, take a look at Spring Reference Documentation Chapter 7. Transaction management
            HTH

            Comment


            • #7
              Hi

              Originally posted by irbouho
              What's wrong with me? Need I have to commit a transaction in spring?
              You need to configure Transaction Management around your dao methods. For more information, take a look at Spring Reference Documentation Chapter 7. Transaction management
              HTH
              Maybe the problem is not this. Sorry for my copy past error in the last post. I find why it couldn't insert data. In the last post ,my code is this:
              Code:
              public Long saveParent&#40;Object o&#41;&#123; 
                    Object obj=null; 
                  
              obj=this.getSession&#40;&#41;.save&#40;o&#41;; <--Exception 
                    log.info&#40;"Saved id&#58; "+obj&#41;; 
                  
                    return &#40;Long&#41;obj;
              I change my code to the following,it can work!
              Code:
              public Long saveParent&#40;Object o&#41;&#123; 
                    Object obj=null; 
                  
              obj=this.getHibernateTemplate&#40;&#41;.save&#40;o&#41;; <--Exception 
                    log.info&#40;"Saved id&#58; "+obj&#41;; 
                  
                    return &#40;Long&#41;obj;
              I also delete the code in my config file:
              Code:
              <!--
              <property name="defaultAutoCommit"> 
                          <value>false</value> 
                      </property>
              -->
              The problem maybe I set the defaultAutoCommit to false. ;-)

              Comment

              Working...
              X