i new spring+hibernate , working on basic web project. simple form used , has 2 fields username , password. has submit button. when clicking on submit supposed check if username , password exists in database or not.this authenticationservice class
package com.service; import java.util.list; import org.hibernate.sessionfactory; import org.springframework.orm.hibernate3.hibernatetemplate; import org.springframework.stereotype.service; @service public class authenticateservice{ private hibernatetemplate hibernatetemplate; public void setsessionfactory(sessionfactory sessionfactory) { this.hibernatetemplate=new hibernatetemplate(sessionfactory); } public boolean verifyusernameandpassword(string username,string password) { system.out.println("into service class"); boolean userstatus=false; try { list userobj = hibernatetemplate.find("from user u u.username=? , u.password=?",username,password); if(userobj.size()!=0) { userstatus=true; } } catch(exception e) { e.printstacktrace(); } return userstatus; } }
this bean configuration file:
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="datasourcebean" class="org.springframework.jdbc.datasource.drivermanagerdatasource"> <property name="driverclassname" value="com.mysql.jdbc.driver"></property> <property name="url" value="jdbc:mysql://127.0.0.1:3306/springmvc"></property> <property name="username" value="root"></property> <property name="password" value=""></property> </bean> <!-- org.springframework.orm.hibernate3.localsessionfactorybean --> <bean id="sessionfactorybean" class="org.springframework.orm.hibernate3.localsessionfactorybean"> <property name="datasource" ref="datasourcebean"></property> <property name="mappingresources"> <value>com/pojo/user.hbm.xml</value> </property> <property name="hibernateproperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.mysql5dialect</prop> <prop key="hibernate.show_sql">true</prop> </props> </property> </bean> <bean id="hibernatetemplatebean" class="org.springframework.orm.hibernate3.hibernatetemplate"> <property name="sessionfactory" ref="sessionfactorybean"></property> </bean> <bean id="authenticateservicebean" class="com.service.authenticateservice"> <property name="hibernatetemplate" ref="hibernatetemplatebean"></property> </bean> </beans>
this login controller:
package com.controller; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; import org.springframework.web.bind.annotation.requestparam; import org.springframework.web.servlet.modelandview; import com.service.authenticateservice; @controller @requestmapping("/login.spring") public class logincontroller { @autowired authenticateservice authenticateservice; @requestmapping(method= requestmethod.post) public modelandview processcredentials(@requestparam("username")string username ,@requestparam("password")string password) { system.out.println("inside username"+username + " password"+password); string message="invalid credentials"; if(authenticateservice.verifyusernameandpassword(username, password)==true) { message="welcome"+username; } return new modelandview("results","message",message); } }
when run application following stacktrace :
java.lang.nullpointerexception @ com.service.authenticateservice.verifyusernameandpassword(authenticateservice.java:31) @ com.controller.logincontroller.processcredentials(logincontroller.java:26) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:39) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:25) @ java.lang.reflect.method.invoke(method.java:597) @ org.springframework.web.bind.annotation.support.handlermethodinvoker.invokehandlermethod(handlermethodinvoker.java:176) @ org.springframework.web.servlet.mvc.annotation.annotationmethodhandleradapter.invokehandlermethod(annotationmethodhandleradapter.java:440) @ org.springframework.web.servlet.mvc.annotation.annotationmethodhandleradapter.handle(annotationmethodhandleradapter.java:428) @ org.springframework.web.servlet.dispatcherservlet.dodispatch(dispatcherservlet.java:925) @ org.springframework.web.servlet.dispatcherservlet.doservice(dispatcherservlet.java:856) @ org.springframework.web.servlet.frameworkservlet.processrequest(frameworkservlet.java:936) @ org.springframework.web.servlet.frameworkservlet.dopost(frameworkservlet.java:838) @ javax.servlet.http.httpservlet.service(httpservlet.java:637) @ org.springframework.web.servlet.frameworkservlet.service(frameworkservlet.java:812) @ javax.servlet.http.httpservlet.service(httpservlet.java:717) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:290) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:206) @ org.apache.catalina.core.standardwrappervalve.invoke(standardwrappervalve.java:233) @ org.apache.catalina.core.standardcontextvalve.invoke(standardcontextvalve.java:191) @ org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.java:127) @ org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.java:103) @ org.apache.catalina.core.standardenginevalve.invoke(standardenginevalve.java:109) @ org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.java:293) @ org.apache.coyote.http11.http11processor.process(http11processor.java:861) @ org.apache.coyote.http11.http11protocol$http11connectionhandler.process(http11protocol.java:606) @ org.apache.tomcat.util.net.jioendpoint$worker.run(jioendpoint.java:489) @ java.lang.thread.run(thread.java:662)
i sure application goes service class have printed "inside service class". don't know why giving me nullpointer.
you have 2 beans of type "com.service.authenticateservice" 1 via annotation in xml file.
the 1 defined via annotation not provided hibernate template reference, hence null. 1 defined in xml having proper reference not 1 gets injected.
hope helps.
Comments
Post a Comment