HTTP状态500 - NullPointerException

时间:2015-06-28 03:21:51

标签: spring nullpointerexception

我的JSP中有以下表单。它返回HTTP状态500。

<form:form action="poll1" modelAttribute="poll1" method="post">
                <table>
                    <tr>
                        <td><b><i>Poll #1</i></b></td>
                    </tr>
                    <tr>
                        <td>Would you like to have a 30-year reunion in 2016?<br>
                        </td>
                    </tr>
                    <tr>
                        <td><form:radiobutton path="vote" value="yes" />Yes <form:radiobutton
                                path="vote" value="no" />No</td>
                    </tr>
                    <tr>
                        <td><input name="submit" type="submit"
                            value="Vote Poll #1" align="left" /></td>
                    </tr>
                </table>
            </form:form>

它映射到我的控制器类中的以下方法,并且我已经标记了我得到NullPointerException的行。

@RequestMapping(value = "/poll1", method = RequestMethod.POST)
public String processPoll1(@RequestParam String vote,
        HttpServletResponse response, Model model) {
    Map<String, Object> resultMap = poll1DAO.tallyVote(vote);
    Cookie poll1 = new Cookie("poll1", "voted");
    // Next line has the error
    model.addAttribute("poll1Yes", resultMap.get("yes").toString());
    model.addAttribute("poll1No", resultMap.get("no").toString());
    poll1.setMaxAge(maxSeconds);
    response.addCookie(poll1);
    return "redirect:/polls";
}

以下代码来自我的DAO课程,我相信它会返回null,但我无法弄清楚原因。

public Map<String, Object> tallyVote(String vote) {
    int votes;

    try {
        votes = this.getJdbcTemplate().queryForInt(
                "select " + vote + " from poll1 where id = 1") + 1;
        this.getJdbcTemplate().update(
                "update poll1 set " + vote + " = " + votes
                        + " where id = 1");
        Map<String, Object> returnMap = getVotes();
        return returnMap;
    } catch (DataAccessException e) {
        System.out.println(e.toString());
    }

    return null;
}

这是我的异常堆栈。

exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
    org.springframework.web.servlet.FrameworkServlet.finalizeProcessing(FrameworkServlet.java:947)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:893)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:792)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause

java.lang.NullPointerException
    com.controller.PollController.processPoll1(PollController.java:65)
    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:483)
    org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:647)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:603)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:950)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:859)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:883)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:792)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

2 个答案:

答案 0 :(得分:0)

您可以将调试器设置为此行之前的行吗?

Error: [$parse:syntax] Syntax Error: Token ':' is an unexpected token at column 17 of the expression [background-color:]

我怀疑resultMap.get(“yes”)是返回null的部分。然后你调用toString(),导致NPE)。 特别是因为你有     地图resultMap = poll1DAO.tallyVote(投票); 这可能会返回一张空地图。

完全确定您需要将调试器放在您标记的行的正上方。 查看本教程http://www.wikijava.org/wiki/Debugging_a_servlet_with_tomcat_and_Eclipse_tutorial,了解有关如何调试servlet的更多详细信息。

答案 1 :(得分:0)

我已经解决了自己的问题。我有不正确的数据库URL和登录信息。提供正确的信息解决了问题。感谢所有回复的人。