HTTP状态500 - 请求处理失败;调用服务层方法时,嵌套异常是java.lang.NullPointerException

时间:2016-07-12 09:20:00

标签: spring-mvc nullpointerexception service-layer

调用userAuth(String UserId)方法时,我遇到NullPointerException。

以下是我的项目文件以及错误快照。

Error:

Directory Structure

LoginController.java

package com.skit.app.controller;

import java.util.List;

import javax.servlet.Servlet;
import javax.servlet.ServletContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.skit.app.dao.ProductDao;
import com.skit.app.dao.UserDao;
import com.skit.app.model.Product;
import com.skit.app.model.User;
import com.skit.app.services.LoginService;
import com.skit.app.services.LoginServiceImpl;

@Controller
@SessionAttributes("name")
public class LoginContoller {

LoginService service;
@RequestMapping("/login")
public String login() {
    return "login";
}



@RequestMapping(value = "/userCheck", method = RequestMethod.POST)
public String userCheck(javax.servlet.http.HttpServletRequest request, ModelMap model) {
    String view="./";
    String name = request.getParameter("user");
    String pwd = request.getParameter("pass");
    String usertype = request.getParameter("radios");

        User user ; 
        user=service.userAuth(name); //This Line is causing the Exception

        if (pwd.equals(user.getU_password()) && name.equals(user.getU_id())) {

            if (usertype.equals("seller")) {

                model.put("message", user.getU_name());
                model.put("name", name);
                view= "seller";
            } else {
                /*ProductDao productDao = (ProductDao) context.getBean("productDAO");
                List<Product> plist = productDao.viewProduct();
                model.put("name", name);
                model.put("plist", plist);*/
                model.put("message", user.getU_name());
                model.put("cart", new Product());
                view= "User";

            }
        } else {
            // If password is incorrect
            model.put("error", true);
            view="login";
        } 

    return view;
}
}

UserDao.java

package com.skit.app.dao;

import com.skit.app.model.User;

public interface UserDao {

public User userAuth(String userId);

public int userReg(User user);

public int userUpdate(User user);
}

UserDaoImpl.java

package com.skit.app.dao.impl;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.skit.app.dao.UserDao;
import com.skit.app.model.*;



public class UserDaoImpl  extends JdbcDaoSupport implements UserDao{


public User userAuth(String userId) throws EmptyResultDataAccessException
{

    System.out.println(userId);
    User user=new User();
    String sql = "SELECT * FROM user where u_id=?";
    Object[] inputs = new Object[] {userId};
    user=(User) getJdbcTemplate().queryForObject(sql, inputs, new                      BeanPropertyRowMapper(User.class));
    return user;

}
public int userReg(User user)
{
    String sql = "INSERT INTO USER VALUES(?, ?, ?, ?, ?, ?)";
    Object[] inputs = new Object[] {""+user.getU_id(), ""+user.getU_type(),   ""+user.getU_name(), ""+user.getU_password(),""+user.getU_email(),""+user.getU_mobno()};
    return getJdbcTemplate().update(sql, inputs);

}

public int userUpdate(User user)
{
    String sql = "update user set u_name=?, u_email=?, u_mobno=? where u_id=?;";
    Object[] inputs = new Object[] {""+user.getU_name(),""+user.getU_email(),""+user.getU_mobno(),""+user.getU_id()};
    return getJdbcTemplate().update(sql,inputs);

}

LoginService.java

package com.skit.app.services;

import com.skit.app.model.User;

public interface LoginService{

public User userAuth(String userId);

public int userReg(User user);

public int userUpdate(User user);
}

LoginServiceImpl.java

    package com.skit.app.services;


import org.springframework.stereotype.Service;

import com.skit.app.dao.UserDao;
import com.skit.app.model.User;



public class LoginServiceImpl implements LoginService{


    UserDao dao;
    User user;
    public User userAuth(String userId) {
        user= dao.userAuth(userId);
        System.out.println(user);
        return user;
    }

    public int userReg(User user) {
        // TODO Auto-generated method stub
        return 0;
    }

    public int userUpdate(User user) {
        // TODO Auto-generated method stub
        return 0;
    }


}

1 个答案:

答案 0 :(得分:1)

您错过了LoginServiceImpl上的@Service注释。

@Service
public class LoginServiceImpl implements LoginService{

您错过了控制器中的@Autowire注释,以自动维护服务。

public class LoginContoller {
@Autowire
LoginService service;

还要确保正确配置组件扫描