获取会话时的NullPointerexception

时间:2013-05-14 07:17:48

标签: spring-mvc

我收到以下异常。由于行

Session  ss=session.getCurrentSession();

java.lang.NullPointerException
    at com.mkyong.common.dao.UserDao.saveUser(UserDao.java:25)
    at com.mkyong.common.controller.JSONController.getShopInJSON(JSONController.java:31)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:722)

我的dao课程如下。

 package com.mkyong.common.dao;
import org.hibernate.SessionFactory;
import org.hibernate.Session;
import com.mkyong.common.model.User;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

public class UserDao {

    private SessionFactory session;

    public void setSessionFactory(SessionFactory session){
        if(session == null){
         throw new IllegalStateException();
        }
        this.session=session;
    }


    public int saveUser(User user) {

        Session  ss=session.getCurrentSession();
        System.out.println(user.getUserName());
        System.out.println(user.getPassword());
        //Session ss=session.getCurrentSession();
        ss.save(user);



        return 1; 



}


}

我的配置文件如下:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
    <context:property-placeholder location="classpath:jdbc.properties" />
    <context:component-scan base-package="com.mkyong.common.controller" />
    <context:annotation-config/>

        <mvc:annotation-driven />





    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${database.driver}" />
        <property name="url" value="${database.url}" />
        <property name="username" value="${database.user}" />
        <property name="password" value="${database.password}" />
    </bean>

     <bean id="SessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" /> 

        <property name="annotatedClasses">
            <list>
                <value>com.mkyong.common.model.User</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
            </props>
        </property>
    </bean>
    <bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="SessionFactory" ref="SessionFactory" />
    </bean> 


</beans>

2 个答案:

答案 0 :(得分:0)

您的session为空。您在getCurrentSession上使用null。因此,您在此处获得NullpointeException

答案 1 :(得分:0)

您的会话未在您的DAO中注入。确保您的配置正确。

您可以通过两种方式注入会话工厂(仅选择一种)

1)Xml声明 在这里,您将以这种方式在xml配置文件中声明userDAO bean:

<bean id="userDAO" class="com.mkyong.common.dao.UserDAO">
    <property name="session" ref="SessionFactory" />
</bean>

当我的bean的id以大写字母开头时,我有时会遇到问题,因此我会将“SessionFactory”重命名为“sessionFactory”。

2)带注释

在这里,Spring将扫描用

声明的所有包
<context:component-scan base-package="com.foo.bar" />

并在com.foo.bar包中查找使用“@Repository”,“@ Service”或“@Component”注释的类。因此,您的类UserDAO应该使用“@Repository”进行注释,并且您的包必须在组件扫描中进行扫描。 现在Spring将检测你的userDAO bean,但是没有看到任何要注入的bean,所以你必须用“@Autowired”注释你的SessionFactory字段,你的类必须如下所示:

@Repository
public class UserDao {

@Autowired
private SessionFactory session;

public void setSessionFactory(SessionFactory session){
    if(session == null){
     throw new IllegalStateException();
    }
    this.session=session;
}

...

您的配置文件如下所示:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans     
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:property-placeholder location="classpath:jdbc.properties" />
<context:component-scan base-package="com.mkyong.common.controller" />
<context:component-scan base-package="com.mkyong.common.dao" />
<context:annotation-config/>

    <mvc:annotation-driven />

...

</beans>