spring mvc - NullPointerexception in getting session -
i getting following exception.its due line
session ss=session.getcurrentsession(); java.lang.nullpointerexception @ com.mkyong.common.dao.userdao.saveuser(userdao.java:25) @ com.mkyong.common.controller.jsoncontroller.getshopinjson(jsoncontroller.java:31) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:57) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:601) @ org.springframework.web.bind.annotation.support.handlermethodinvoker.invokehandlermethod(handlermethodinvoker.java:176) @ org.springframework.web.servlet.mvc.annotation.annotationmethodhandleradapter.invokehandlermethod(annotationmethodhandleradapter.java:426) @ org.springframework.web.servlet.mvc.annotation.annotationmethodhandleradapter.handle(annotationmethodhandleradapter.java:414) @ org.springframework.web.servlet.dispatcherservlet.dodispatch(dispatcherservlet.java:790) @ org.springframework.web.servlet.dispatcherservlet.doservice(dispatcherservlet.java:719) @ org.springframework.web.servlet.frameworkservlet.processrequest(frameworkservlet.java:644) @ org.springframework.web.servlet.frameworkservlet.dopost(frameworkservlet.java:560) @ javax.servlet.http.httpservlet.service(httpservlet.java:647) @ javax.servlet.http.httpservlet.service(httpservlet.java:728) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:305) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:210) @ org.apache.catalina.core.standardwrappervalve.invoke(standardwrappervalve.java:222) @ org.apache.catalina.core.standardcontextvalve.invoke(standardcontextvalve.java:123) @ org.apache.catalina.authenticator.authenticatorbase.invoke(authenticatorbase.java:472) @ org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.java:171) @ org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.java:99) @ org.apache.catalina.valves.accesslogvalve.invoke(accesslogvalve.java:936) @ org.apache.catalina.core.standardenginevalve.invoke(standardenginevalve.java:118) @ org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.java:407) @ org.apache.coyote.http11.abstracthttp11processor.process(abstracthttp11processor.java:1004) @ org.apache.coyote.abstractprotocol$abstractconnectionhandler.process(abstractprotocol.java:589) @ org.apache.tomcat.util.net.jioendpoint$socketprocessor.run(jioendpoint.java:310) @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1145) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:615) @ java.lang.thread.run(thread.java:722)
my dao class 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 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>
your session
null.you using getcurrentsession
on null
.so getting nullpointeexception here
Comments
Post a Comment