request.getParameterValues()问题

赵小宾 发布于 2013/08/22 16:49
阅读 1K+
收藏 0
在一个JSP页面中,多个<input>的name一样,在servlet中用Map<Integer,String[]> map = new HashMap<Integer, String[]>();
map.put(id, request.getParameterValues(idStr));//这里的idStr就是
                                                //<input> 的name
为什么
for(int i=0;i<map.size();i++){
System.out.println(map.size());
System.out.println(map.get(i).length);
/*for(int j=0;j<str.length;j++){
System.out.println(str[j]);
}*/
}

会报错?

2013-8-22 16:33:41 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet AddAnswerSheetServlet threw exception
java.lang.NullPointerException
	at com.answerSheetServlet.AddAnswerSheetServlet.doPost(AddAnswerSheetServlet.java:93)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at com.util.ChinaFilter.doFilter(ChinaFilter.java:16)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
	at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:879)
	at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:600)
	at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1703)
	at java.lang.Thread.run(Thread.java:619)

加载中
0
QAllen
QAllen
直接map.get(id)不就拿出来string[]数组了吗 然后再遍历数组。
0
偶是小娃
偶是小娃
map.get(i).length这边报空指针了,map.get(i)取出来的是空的。另外这样迭代map不对的。map.get(key)中的参数是代表key值,而不应该是你现在这个迭代里面的变量值。
赵小宾
赵小宾
恩!你说的很对!那应该怎样写呢?
0
乌鸦哥
乌鸦哥
Map<Integer, String[]> map = new HashMap<Integer, String[]>();
for (Entry<Integer, String[]> entry : map.entrySet()) {
  System.out.println(entry.getKey());

   System.out.println(entry.getValue());

   这这里判断一下entry.getValue() 的值,然后循环,注意空校验

}
返回顶部
顶部