【已解决】org.springframework.web.util.NestedServletException: Request processing failed; nested exceptio

exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)
	org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
	org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

root cause

java.lang.NullPointerException
	com.rgl.controller.UserController.login(UserController.java:48)
	sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	java.lang.reflect.Method.invoke(Method.java:498)
	org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:220)
	org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:134)
	org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:116)
	org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
	org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
	org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
	org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
	org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
	org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
	org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

=============================分割线=============================================
之所以出现这个Exception,是因为我用【User user =this.userService.....】这句话对user进行了初始化,但等号【=】后面的句子执行失败,最后导致对象初始化失败。
解决方法:等号后面的句子有问题。经过检查,发现调用的方法在一个接口类中,但我并没有注入那个被调用的类,因此,只需要进行注入就好了。
    在UserController中加上@Autowired就行了。源代码如下所示:


 修正前:

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.rgl.domain.User;
import com.rgl.service.IUserService;

@Controller
@RequestMapping("/user")
public class UserController {

    public IUserService userService;
        
    @RequestMapping("/signIn.do")
    public String login(HttpServletRequest httpServletRequest,Model model) throws Exception{
        String username=httpServletRequest.getParameter("username");
        String password=httpServletRequest.getParameter("password");
        /*try{
            User user=this.userService.selectByNamePassword(username, password);
            model.addAttribute("user", user);
            return "user/showUser";
        }catch (Exception e) {
            // TODO: handle exception
            return "fail";
        }*/
        User user=this.userService.selectByNamePassword(username, password);
        System.out.print(user.getUserid()+":"+user.getName());
        model.addAttribute("user", user);
        return "user/showUser";
    }
}

修正后:

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.rgl.domain.User;
import com.rgl.service.IUserService;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    public IUserService userService;
    
    @RequestMapping("/signIn.do")
    public String login(HttpServletRequest httpServletRequest,Model model) throws Exception{
        String username=httpServletRequest.getParameter("username");
        String password=httpServletRequest.getParameter("password");
        /*try{
            User user=this.userService.selectByNamePassword(username, password);
            model.addAttribute("user", user);
            return "user/showUser";
        }catch (Exception e) {
            // TODO: handle exception
            return "fail";
        }*/
        User user=this.userService.selectByNamePassword(username, password);
        System.out.print(user.getUserid()+":"+user.getName());
        model.addAttribute("user", user);
        return "user/showUser";
    }
}



 

你可能感兴趣的:(web开发,spring,mvc,Bugs,&,Errors,web开发)