0

I am very new to Spring+Hibernate and am working on a very basic web project. Simple form is used and has two fields username and password. It has a submit button. When clicking on submit I am supposed to check if username and password exists in the database or not.This is my 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 the Service Class");
        boolean userStatus=false;
        try
        {

            List userObj = hibernateTemplate.find("from user u where u.username=? and u.password=?",username,password);     
            if(userObj.size()!=0)
            {
                userStatus=true;
            }

        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

        return userStatus;
    }

}

This is my 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 is my 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 I run the application I get the following stacktrace :

java.lang.NullPointerException
    at com.service.AuthenticateService.verifyUsernameAndPassword(AuthenticateService.java:31)
    at com.controller.LoginController.processCredentials(LoginController.java:26)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:440)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:428)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:838)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:662)

I am sure the application goes to the Service class as I have printed the "Inside the Service Class". I don't know why its giving me a NullPointer.

2
  • This is nothing to do with HibernateTemplate. Your code is throwing NullPointerException, on line 31 of AuthenticateService, as the stack trace clearly shows.
    – artbristol
    Jul 11, 2013 at 11:51
  • @artbristol: Could you please tell me where the problem is..? Jul 11, 2013 at 16:47

1 Answer 1

0

You have two beans of type "com.service.AuthenticateService" one via annotation another in xml file.

The one defined via annotation is not provided hibernate template reference, hence null. The one defined in xml is having proper reference but is not the one that gets injected.

Hope this helps.

2
  • So I should either keep the reference or the annotation.? But I thought @Autowired required a bean with the same name in the config file. Jul 11, 2013 at 17:19
  • No its not that way.Its substitute for dependency injection via xml.Kindly refer spring docs for same.You are doing same thing twice, in xml and using annotation,remove service authenticateServiceBean from xml, rename hibernateTemplateBean as hibernateTemplate, in java class AuthenticateService add @Autowired private HibernateTemplate hibernateTemplate. Remove setSessionFactory method. Things also depend on where you have defined beans in main application context xml file or the servlet context xml file. Jul 11, 2013 at 17:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.