把数据库数据显示在JSP界面报错
不知道为什么,莫名报了一个Iterator的错误,但是我查了一下压根没写错,想不明白为什么,只能求助各位了代码如下:
display.jsp
<%@page import="org.apache.catalina.User"%>
<%@page import="com.model.Student"%>
<%@ page import="java.util.List"%>
<%@page import="java.util.Iterator"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>所有学生信息</title>
<jsp:include page="login.jsp"></jsp:include>
<body>
<%
List stus=(List)session.getAttribute("stus");
Iterator iterator=stus.iterator();
%>
<table>
<tr><td>id</td><td>stu_id</td><td>stu_name</td><td>stu_math</td><td>stu_chinese</td><td>stu_english</td></tr>
<%
int i=0;
while(iterator.hasNext())
{
Student student=(Student)iterator.next();
%>
<tr>
<td><%=student.getStu_id() %></td>
<td><%=student.getStu_name() %></td>
<td><%=student.getStu_math() %></td>
<td><%=student.getStu_chinese() %></td>
<td><%=student.getStu_english() %></td>
<td><a href="delete.jsp?id=<%=student.getId() %>">删除</a>
</tr>
<%
}
%>
</table>
</body>
</html>
查询功能的函数
public List<Student> queryAllStu()
{
List<Student> stus=new ArrayList<Student>();
try {
pStatement=connection.prepareStatement("select *from stu_grade");
ResultSet rSet=pStatement.executeQuery();
while (rSet.next())
{
Student student=new Student();
student.setId(rSet.getInt(1));
student.setStu_id(rSet.getString(2));
student.setStu_name(rSet.getString(3));
student.setStu_math(rSet.getString(4));
student.setStu_chinese(rSet.getString(5));
student.setStu_english(rSet.getString(6));
stus.add(student);
}
return stus;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return null;
}
}
报错信息:
严重: Servlet.service() for servlet [jsp] in context with path [/the_experience] threw exception [An exception occurred processing JSP page /display.jsp at line 16
13: <body>
14: <%
15: List stus=(List)session.getAttribute("stus");
16: Iterator iterator=stus.iterator();
17: %>
18: <table>
19: <tr><td>id</td><td>stu_id</td><td>stu_name</td><td>stu_math</td><td>stu_chinese</td><td>stu_english</td></tr>
Stacktrace:] with root cause
java.lang.NullPointerException
at org.apache.jsp.display_jsp._jspService(display_jsp.java:132)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:94)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:492)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1152)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
at org.apache.tomcat.(NioEndpoint.java:1539)
at org.apache.tomcat.(NioEndpoint.java:1495)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)