jsp String hobbys[]=(String[])request.getParameterValues("hobby");复选框取值 数组空指针异常
<inputtype="checkbox"name="hobby"value="Sports"checked>体育<inputtype="checkbox"name="h...
<input type="checkbox" name="hobby" value="Sports" checked>体育
<input type="checkbox" name="hobby" value="Music" >音乐
<input type="checkbox" name="hobby" value="Arts" >文学
<input type="checkbox" name="hobby" value="Others">其他
<input type="text" name="Accountstr" size="10" value="其他爱好" maxlength="15"></td>
</tr>
String hobbys[]=(String[])request.getParameterValues("hobby");
for(int j=0;j<hobbys.length;j++){
if(hobbys[j]==null)
hobbys[j]=" ";
info.add(hobbys[j]);
}
错误提示:
org.apache.jasper.JasperException: An exception occurred processing JSP page /homework/bian.jsp at line 312
309: }
310: info.add(qq);
311:
312: for(int j=0;j<hobbys.length;j++){
313: if(hobbys[j]==null)
314: hobbys[j]=" ";
315: info.add(hobbys[j]);
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:524)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:435)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
root cause
java.lang.NullPointerException
org.apache.jsp.homework.bian_jsp._jspService(bian_jsp.java:367)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803) 展开
<input type="checkbox" name="hobby" value="Music" >音乐
<input type="checkbox" name="hobby" value="Arts" >文学
<input type="checkbox" name="hobby" value="Others">其他
<input type="text" name="Accountstr" size="10" value="其他爱好" maxlength="15"></td>
</tr>
String hobbys[]=(String[])request.getParameterValues("hobby");
for(int j=0;j<hobbys.length;j++){
if(hobbys[j]==null)
hobbys[j]=" ";
info.add(hobbys[j]);
}
错误提示:
org.apache.jasper.JasperException: An exception occurred processing JSP page /homework/bian.jsp at line 312
309: }
310: info.add(qq);
311:
312: for(int j=0;j<hobbys.length;j++){
313: if(hobbys[j]==null)
314: hobbys[j]=" ";
315: info.add(hobbys[j]);
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:524)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:435)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
root cause
java.lang.NullPointerException
org.apache.jsp.homework.bian_jsp._jspService(bian_jsp.java:367)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803) 展开
2个回答
2011-12-01
展开全部
当然报空指针异常了,因为你第一次访问页面的时候,hobby数组没初始化,因为没有选中的嘛,所以,在你的for循环外面还必须加判断,数组是否为空,问题就得以解决了,明白了吗?
追问
if(hobbys!=null){
hobby=hobbys[0];
for(int j=1;j<hobbys.length;j++){
if(hobbys[j]==null)
hobbys[j]=" ";
hobby+=" ";
hobby+=hobbys[j];
}
info.add(hobby);
}
加上了一个判空操作果然好了 谢谢了
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我测试了下 完全没问题啊
<form method="post"
action="SelectBeer.do">
<input type="checkbox" name="hobby" value="Sports" checked>体育
<input type="checkbox" name="hobby" value="Music" >音乐
<input type="checkbox" name="hobby" value="Arts" >文学
<input type="checkbox" name="hobby" value="Others">其他
<input type="text" name="Accountstr" size="10" value="其他爱好" maxlength="15"></td>
</tr>
<br /> <br />
<center>
<input type="submit" />
</center>
</form>
------------------------
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException,ServletException
{
response.setContentType("text/html");
PrintWriter out= response.getWriter();
String hobbys[]=(String[])request.getParameterValues("hobby");
ArrayList<String> info=new ArrayList<String>();
for(int j=0;j<hobbys.length;j++){
if(hobbys[j]==null)
hobbys[j]=" ";
info.add(hobbys[j]);
}
for(String s:info)
{
out.println("you Select "+s +"<br>");
}
}
<form method="post"
action="SelectBeer.do">
<input type="checkbox" name="hobby" value="Sports" checked>体育
<input type="checkbox" name="hobby" value="Music" >音乐
<input type="checkbox" name="hobby" value="Arts" >文学
<input type="checkbox" name="hobby" value="Others">其他
<input type="text" name="Accountstr" size="10" value="其他爱好" maxlength="15"></td>
</tr>
<br /> <br />
<center>
<input type="submit" />
</center>
</form>
------------------------
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException,ServletException
{
response.setContentType("text/html");
PrintWriter out= response.getWriter();
String hobbys[]=(String[])request.getParameterValues("hobby");
ArrayList<String> info=new ArrayList<String>();
for(int j=0;j<hobbys.length;j++){
if(hobbys[j]==null)
hobbys[j]=" ";
info.add(hobbys[j]);
}
for(String s:info)
{
out.println("you Select "+s +"<br>");
}
}
追问
额 可是我这个他总提示NULLPOINTERE。。。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询