在一个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)
直接map.get(id)不就拿出来string[]数组了吗然后再遍历数组。map.get(i).length这边报空指针了,map.get(i)取出来的是空的。另外这样迭代map不对的。map.get(key)中的参数是代表key值,而不应该是你现在这个迭代里面的变量值。恩!你说的很对!那应该怎样写呢?Map<Integer,String[]>map=newHashMap<Integer,String[]>();
for(Entry<Integer,String[]>entry:map.entrySet()){
System.out.println(entry.getKey());
System.out.println(entry.getValue());
这这里判断一下entry.getValue()的值,然后循环,注意空校验
}版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。