为什么在QueryForObject上收到NullPointerException?

时间:2019-05-23 03:24:29

标签: spring spring-mvc jdbc nullpointerexception

我正在使用spring mvc,并尝试使用jdbc创建登录验证。但是,它一直抛出NullPointerException。

我在线查找有关queryForObject的信息,并在我的UserMapper中添加了try-catch块,但这并不能解决该异常。

这是堆栈跟踪:

java.lang.NullPointerException
    com.myWeb.UserService.validateUser(UserService.java:31)
    com.myWeb.LoginController.loginProcess(LoginController.java:26)
    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.invoke(InvocableHandlerMethod.java:214)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:690)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

这是UserService中的函数:

public User validateUser(Login login) {
    String sql = "select * from users where username=? and password=?";
    User u;
    u = jdbcTemplate.queryForObject(sql, new Object[]  {login.getUsername(),login.getPassword()}, new UserMapper());//EXCEPTION HERE
    return u;
}

这些是我的豆子

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">        
    <property name="driverClassName" value="oracle.jdbc.OracleDriver"/>
    <property name="url" value="jdbc:oracle:thin:@localhost:1521:XE"/>
    <property name="username" value="hr"/>
    <property name="password" value="hr"/> 
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource" />
</bean>

这是我的userMapper:

@Override
public User mapRow(ResultSet rs, int arg1) throws SQLException {
    try {
        User user = new User();
        user.setUserName(rs.getString(1));
        user.setPassword(rs.getString(2));
        user.setPhoneNumber(rs.getString(3));
        user.setGender(rs.getString(4));
        return user;
    }
    catch (Exception e){
        return null;
    }
}

这是loginController中的函数:

@RequestMapping(value = "/login", method = RequestMethod.POST)
public ModelAndView loginProcess(HttpServletRequest request, HttpServletResponse response, @ModelAttribute("login") Login login) 
{
    ModelAndView mav = null;
    UserService us = new UserService();
    User user = us.validateUser(login); //EXCEPTION HERE
    if (user != null) {
        mav = new ModelAndView("success");
        mav.addObject("user", user);
    } 
    else {
        mav = new ModelAndView("login");
        mav.addObject("message", "Username or Password is wrong!!");
    }
    return mav;
}

我希望queryForObject如果在数据库中找到一个用户,则返回一个用户;如果找不到用户,则返回null。

0 个答案:

没有答案