Announcement Announcement Module
Collapse
No announcement yet.
this.getSession throws NullPointerException?! Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • this.getSession throws NullPointerException?!

    Hi friends

    I use spring with HibernateDaoSupport class. When I run the following code:
    Code:
    public class UserDAOImpl extends HibernateDaoSupport implements UserDAO {
    .....................................................................................................
    ...............................................................................
    public User createUser(User user, UserDetail detail) {
    User hasuser = (User) this.getHibernateTemplate()
    					.createQuery(
    							this.getSession&#40;&#41;,  <----- NullPointerException here
    							"from User u where u.userName='"
    									+ user.getUserName&#40;&#41; + "'"&#41;.uniqueResult&#40;&#41;;
    &#125;
    .................................................................................................
    Tomcat throws Exception:
    Code:
    root cause 
    
    java.lang.NullPointerException
    	org.springframework.orm.hibernate.support.HibernateDaoSupport.getSession&#40;HibernateDaoSupport.java&#58;141&#41;
    	lyo.hotmail.site.service.UserDAOImpl.createUser&#40;UserDAOImpl.java&#58;41&#41;
    	lyo.hotmail.site.action.RegisteAction.registeUser&#40;RegisteAction.java&#58;51&#41;
    	sun.reflect.NativeMethodAccessorImpl.invoke0&#40;Native Method&#41;
    	sun.reflect.NativeMethodAccessorImpl.invoke&#40;NativeMethodAccessorImpl.java&#58;39&#41;
    	sun.reflect.DelegatingMethodAccessorImpl.invoke&#40;DelegatingMethodAccessorImpl.java&#58;25&#41;
    	java.lang.reflect.Method.invoke&#40;Method.java&#58;324&#41;
    	org.apache.struts.actions.DispatchAction.dispatchMethod&#40;DispatchAction.java&#58;280&#41;
    	org.apache.struts.actions.DispatchAction.execute&#40;DispatchAction.java&#58;216&#41;
    	org.apache.struts.action.RequestProcessor.processActionPerform&#40;RequestProcessor.java&#58;484&#41;
    	org.apache.struts.action.RequestProcessor.process&#40;RequestProcessor.java&#58;274&#41;
    	org.apache.struts.action.ActionServlet.process&#40;ActionServlet.java&#58;1482&#41;
    	org.apache.struts.action.ActionServlet.doPost&#40;ActionServlet.java&#58;525&#41;
    	javax.servlet.http.HttpServlet.service&#40;HttpServlet.java&#58;709&#41;
    	javax.servlet.http.HttpServlet.service&#40;HttpServlet.java&#58;802&#41;
    	lyo.hotmail.site.util.HiberFilter.doFilter&#40;HiberFilter.java&#58;46&#41;
    
    
    note The full stack trace of the root cause is available in the Apache Tomcat/5.0.29 logs.
    My config file is the following :
    Code:
    <beans>
    <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/Hibernate?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/hotmail/site/bean/Article.hbm.xml</value>
            <value>lyo/hotmail/site/bean/Forum.hbm.xml</value>
            <value>lyo/hotmail/site/bean/Message.hbm.xml</value>
            <value>lyo/hotmail/site/bean/User.hbm.xml</value>
            <value>lyo/hotmail/site/bean/UserDetail.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="fproxy" class="lyo.hotmail.site.util.ForumProxy">
       <property name="fdao">
          <ref bean="formDao"/>
       </property>
    </bean>
    <bean id="viewSpringForumController" class="lyo.hotmail.site.action.ViewForumcontroller">
    <property name="forumProxy">
         <ref bean="fproxy"/>
    </property>
       </bean>
       <bean id="rewriteURLController" class="lyo.hotmail.site.action.DispatchForumController">
    <property name="fdao">
          <ref bean="formDao"/>
       </property>
       </bean>
    <bean id="postArticleController" class="lyo.hotmail.site.action.PostArticleController">
    <property name="fdao">
          <ref bean="formDao"/>
       </property>
    </bean>
    <bean id="viewDetailController" class="lyo.hotmail.site.action.viewDetailController">
    <property name="forumProxy">
         <ref bean="fproxy"/>
    </property>
    </bean>
        <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
            <property name="mappings">
                <props>
                    <prop key="/oldForum.spring">viewSpringForumController</prop>
    				<prop key="/viewForum.spring">viewSpringForumController</prop>
    				<prop key="/detail.spring">viewDetailController</prop>
    				<prop key="/newPost.spring">rewriteURLController</prop>
    				<prop key="/post.spring">postArticleController</prop>
                </props>
            </property>
       
        </bean>
    
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="viewClass"><value>org.springframework.web.servlet.view.JstlView</value></property>
            <property name="prefix"><value>/WEB-INF/forum/</value></property>
            <property name="suffix"><value>.jsp</value></property>
        </bean>
    <bean id="formDao" class="lyo.hotmail.site.service.ForumDAOImpl">
       <property name="sessionFactory">
          <ref bean="MySessionFactory"/>
       </property>
       <property name="udao">
          <ref bean="userDao"/>
       </property>
    </bean> 
     <bean id="userDao" class="lyo.hotmail.site.service.UserDAOImpl">
       <property name="sessionFactory">
          <ref bean="MySessionFactory"/>
       </property>
    </bean> 
    </beans>
    Why "this.getSession()" throws NullPointerException?! Help!

  • #2
    From the Javadoc for HibernateDaoSupport.getSession():

    Get a Hibernate Session, either from the current transaction or a new one. The latter is only allowed if the "allowCreate" setting of this bean's HibernateTemplate is true.
    Either rely on a thread-bound Session (via HibernateInterceptor), or use it in combination with closeSessionIfNecessary.
    It seems you don't have a Session in scope, so it's null.

    Comment


    • #3
      Code:
      public class UserDAOImpl extends HibernateDaoSupport implements UserDAO &#123;
      .....................................................................................................
      ...............................................................................
      public User createUser&#40;User user, UserDetail detail&#41; &#123;
      User hasuser = &#40;User&#41; this.getHibernateTemplate&#40;&#41;
                     .createQuery&#40;
                           this.getSession&#40;&#41;,  <----- NullPointerException here
                           "from User u where u.userName='"
                                 + user.getUserName&#40;&#41; + "'"&#41;.uniqueResult&#40;&#41;;
      &#125;
      .................................................................................................
      I'm going to rewrite your code to show you how I would deal with this:

      Code:
      public User createUser&#40;User user, UserDetail detail&#41; &#123;
      User hasuser = &#40;User&#41; this.getHibernateTemplate&#40;&#41;
                     .execute&#40;
                         new HibernateCallback&#40;&#41; &#123;
                             public Object doInHibernate&#40;Session session&#41; throws HibernateException &#123;
                                  return createQuery&#40;session,  /* <----- __NO__ NullPointerException here */
                                      "from User u where u.userName='" +
                                      user.getUserName&#40;&#41; +
                                      "'"&#41;.uniqueResult&#40;&#41;;
                             &#125;
                         &#125;
                     &#41;;
      &#125;
      I use the callback mechanism offered by HibernateTemplate. HibernateTemplate provides a Hibernate session, you - the developer - don't have to worry about opening the session or closing it or doing something when an exception occurs. This is approach is superior to getting the Hibernate session yourself.

      Comment


      • #4
        Thks

        Thank you . It works well. I should use HibernateCallback if I want to use session myself. It really is a good taste

        Comment

        Working...
        X