for循环抛出异常

来源:3-7 request对象(上)

qq_快乐的人_0

2018-07-10 09:46

<%@ 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回答

  • 我是无知怎么了
    2018-08-17 10:16:33

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

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

  • qq_wo想_0
    2018-07-11 16:03:42
    加一句判断非空 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;");
          }
     
     }
  • qq_wo想_0
    2018-07-11 15:48:26
    favorite和表单里用的name名称一样吗
  • ghcg
    2018-07-11 15:38:01
    favorites可能为空,先用if判断一下是否为空


JAVA遇见HTML——JSP篇

Java Web入门级教程JSP,带你轻松的学习JSP基础知识

248278 学习 · 3071 问题

查看课程

相似问题