0

I have been following DZONE example of how to create a simple web application with login and registration features using Spring MVC, but however I am struggling with one issue. Tomcat throws an exception in DAO Implementation class, saying

SEVERE: Servlet.service() for servlet [spring] in context with path [/PortfolioWebApp] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause java.lang.NullPointerException

I`ve tried changing spring config but nothing happened. Maybe someone could help me out with my problem. This is DAO Implementation class:

package org.madness.java.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.madness.java.model.Login;
import org.madness.java.model.User;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

public class JdbcDaoImpl extends JdbcDaoSupport{

public User validateAccount(Login login) {
    String sql = "SELECT * FROM Accounts WHERE USERNAME = '"+login.getUsername()+"' AND PASSWORD = '"+login.getPassword()+"'";
    List<User> users = this.getJdbcTemplate().query(sql, new UserMapper());
    if (users.size() > 0) {
        return users.get(0);
    }else {
        return null;
    }

}

private static class UserMapper implements RowMapper<User>{

    @Override
    public User mapRow(ResultSet rs, int arg1) throws SQLException {
        User user = new User();

        user.setId(rs.getLong("ID"));
        user.setUsername(rs.getString("USERNAME"));
        user.setPassword(rs.getString("PASSWORD"));
        user.setFirstname(rs.getString("FIRSTNAME"));
        user.setSurname(rs.getString("SURNAME"));
        user.setEmail(rs.getString("EMAIL"));
        user.setAddress(rs.getString("ADDRESS"));

        return null;
    }

}

}

This is Login Controller:

package org.madness.java.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.madness.java.model.User;
import org.madness.java.model.Login;
import org.madness.java.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class LoginController {
    private UserService userService;

    @RequestMapping(value="/login", method=RequestMethod.GET)
    public ModelAndView showLogin(HttpServletRequest req, HttpServletResponse res) {
        ModelAndView mav = new ModelAndView("login");
        mav.addObject("login", new Login());

        return mav;
    }

    @RequestMapping(value="/loginProcess", method=RequestMethod.POST)
    public ModelAndView loginProcess(HttpServletRequest req, HttpServletResponse res, @ModelAttribute("login") Login login) {
        ModelAndView mav = null;
        if(login != null) {
            userService = new UserService();
            User user = userService.validateUser(login);
            if(user != null) {
                mav = new ModelAndView("home");
                mav.addObject("firstname", user.getFirstname());
            }else {
                mav = new ModelAndView("login");
                mav.addObject("message", "Error: Provided username or password are incorrect. Please, try again...");
            }
        }else {
            mav = new ModelAndView("login");
            mav.addObject("message", "Something happened inside DAO and Service!");
        }
        return mav;
    }
}

This is Spring Config XML file:

    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
       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.xsd">
    <context:component-scan base-package="org.madness.java" />
    <context:annotation-config />

<bean name="userService" class="org.madness.java.service.UserService" />

<bean name="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://192.168.0.27:3306/spring" />
    <property name="username" value="student" />
    <property name="password" value="password" />
</bean>

<bean name="jdbcDaoImpl" class="org.madness.java.dao.JdbcDaoImpl">
    <property name="dataSource" ref="datasource" />
</bean>

And this is UserService:

package org.madness.java.service;

import org.madness.java.dao.JdbcDaoImpl;
import org.madness.java.dao.UserDaoImpl;
import org.madness.java.model.Login;
import org.madness.java.model.User;
import org.springframework.beans.factory.annotation.Autowired;

public class UserService {

    @Autowired
    private JdbcDaoImpl dao;

    public User validateUser(Login login) {
        dao = new JdbcDaoImpl();
        return dao.validateAccount(login);

    }

}
alex_z
  • 416
  • 5
  • 12
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – M. le Rutte Oct 15 '17 at 18:34
  • This is about Spring MVC problem, not the NullPointerException itself – alex_z Oct 15 '17 at 18:36
  • Then edit your question, because now you include the error `nested exception is java.lang.NullPointerException] with root cause java.lang.NullPointerException` – M. le Rutte Oct 15 '17 at 18:38
  • 1. you put your code for the `UserDaoImpl` class, yet you are not using it in the Controller class and it is not even specified in your xml configuration. 2. I looked at the example and there is no code about the `UserService` class/interface, however it is used in the Controller class. How did you implement the `UserService`? – Merve Sahin Oct 15 '17 at 19:56
  • I have updated my question with UserService file and using JdbcDaoSupport file. But it did not resolved my problem – alex_z Oct 15 '17 at 20:05

1 Answers1

0

You need to autowire UserService rather than instantiating it manually.

public class LoginController {
    @Autowired
    private UserService userService;

    ...
    @RequestMapping(value="/loginProcess", method=RequestMethod.POST)
    public ModelAndView loginProcess(HttpServletRequest req, HttpServletResponse res, @ModelAttribute("login") Login login) {
        ModelAndView mav = null;
        if(login != null) {
            userService = new UserService(); //remove this !

Then the instance initialized by Spring will be used where the DAO is accessible etc.

StanislavL
  • 56,971
  • 9
  • 68
  • 98