为了账号安全,请及时绑定邮箱和手机立即绑定

for循环抛出异常

<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body>

<h1>request内置对象</h1>

<%

  request.setCharacterEncoding("utf-8");

%>

 用户名:<%=request.getParameter("usename") %><br>

 爱好:<% 

 String[] favorites=request.getParameterValues("favorite"); 

 for(int i=0;i<favorites.length;i++)

 {

out.println(favorites[i]+"&nbsp;&nbsp;&nbsp;&nbsp;");

 }

 %>

</body>

</html>



HTTP Status 500 – Internal Server Error


Type Exception Report

Message An exception occurred processing JSP page [/request.jsp] at line [14]

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

org.apache.jasper.JasperException: An exception occurred processing JSP page [/request.jsp] at line [14]

11:  用户名:<%=request.getParameter("usename") %><br>
12:  爱好:<% 
13:  String[] favorites=request.getParameterValues("favorite"); 
14:  for(int i=0;i<favorites.length;i++)
15:  {
16: 	 out.println(favorites[i]+"&nbsp;&nbsp;&nbsp;&nbsp;");
17:  }


Stacktrace:
	org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:584)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:481)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

Root Cause

java.lang.NullPointerException
	org.apache.jsp.request_jsp._jspService(request_jsp.java:126)
	org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:443)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

Note The full stack trace of the root cause is available in the server logs.


Apache Tomcat/8.0.0


正在回答

4 回答

你的上一个跳转页面有信息没有填,造成空指针异常,

空指针异常不能捕捉,解决方法就是将容易为空的字段加上非空判断,阻止此字段为空时运行代码造成报错。

1 回复 有任何疑惑可以回复我~
加一句判断非空 if(request.getparameterValues(favorite)!=null)
 {
    String[] favorites=request.getParameterValues("favorite"); 
      for(int i=0;i<favorites.length;i++)
      {
        out.println(favorites[i]+"&nbsp;&nbsp;&nbsp;&nbsp;");
      }
 
 }
0 回复 有任何疑惑可以回复我~
favorite和表单里用的name名称一样吗
0 回复 有任何疑惑可以回复我~
favorites可能为空,先用if判断一下是否为空


1 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

for循环抛出异常

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信