Spring HTTP状态500—NullPointerException
我的JSP中有以下表单。它正在返回HTTP状态500Spring HTTP状态500—NullPointerException,spring,nullpointerexception,Spring,Nullpointerexception,我的JSP中有以下表单。它正在返回HTTP状态500 <form:form action="poll1" modelAttribute="poll1" method="post"> <table> <tr> <td><b><i>Poll #1</i></b></td>
<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>
你能把调试器设置在这一行之前吗
model.addAttribute("poll1Yes", resultMap.get("yes").toString());
我怀疑resultMap.get(“yes”)是返回null的部分。然后调用toString(),导致一个NPE)。
尤其是因为你有
Map resultMap=poll1DAO.tallyVote(投票);
这可能会返回一个空地图
为了完全确定,您需要将调试器放在您标记的行的正上方。
查看本教程,了解有关如何调试servlet的更多详细信息。我已经解决了自己的问题。我的URL和数据库登录信息不正确。提供正确的信息解决了问题。感谢所有响应者。请上传异常堆栈。您的代码非常危险:如果遇到SQL注入:字符串投票可以是任何字符串(从黑客的角度看),并立即连接到您的查询中。所以从理论上讲,delete语句可以发送到数据库中,或者随机插入,等等。
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)
model.addAttribute("poll1Yes", resultMap.get("yes").toString());