1

I am getting the following exception.Its due to the line

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)

My dao class is as follows.

 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; 



}


}

My config file is as follows:

<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>
4

2 回答 2

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>
于 2013-05-14T07:26:12.633 回答
0

Your session is null.You are using getCurrentSession on null .So you are getting NullpointeException here

于 2013-05-14T07:19:38.833 回答