NullPointerexception获取会话

我得到以下例外。它由于线

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>

您的sessionnull 。您在null上使用getCurrentSession 。因此,您在此处收到NullpointeException


您的会话不会注入您的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”注解的Classes。 所以,您的类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>
链接地址: http://www.djcxy.com/p/27883.html

上一篇: NullPointerexception in getting session

下一篇: Java+Spring: SEVERE Servletservice