首页 新闻 会员 周边 捐助

为什么报java.lang.NullPointerException

0
[已解决问题] 解决于 2018-08-27 14:10
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="entity.Student"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'index.jsp' starting page</title>
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->
  </head>
 
  <body>
    <table  align="center" border="1" cellpadding="0" cellspacing="0" width="650">
     <caption>
      <h2>学生信息</h2>
      <a href="insertStudent.jsp">添加学生</a>
     </caption>
     <tr style="background-color:green">
      <th>id</th>
      <th>name</th>
      <th>sex</th>
      <th>weight</th>
      <th>bri</th>
      <th>操作</th>
     </tr>
     <%
      Object obj=request.getAttribute("list");
         List<Student> stus=(List<Student>)obj;
         for(Student s : stus){
          %>
          <TR>
           <td><%=s.getSid()%></td>
           <td><%=s.getSname()%></td>
           <td><%=s.getSex()%></td>
           <td><%=s.getWeight()%></td>
           <td><%=s.getBri()%></td>
           <td>
            <a href="">删除</a>
            <a href="">修改</a>
           </td>
          </TR>
          <%
         }
     %>
    </table>
  </body>
</html>
 
____________________________________________
 
package Dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import entity.Student;
public class StudentDao {
 public List queryStudentAll() {
  Connection conn = BaseDao.getConn();
  Statement stat =null;
  ResultSet rs = null;
  ArrayList<Student> list = new ArrayList<Student>();
  try {
    stat = conn.createStatement();
    rs = stat.executeQuery("select * from student");
   while (rs.next()) {
    Student s = new Student();
    s.setSid(rs.getInt("sid"));
    s.setSname(rs.getString("sname"));
    s.setSex(rs.getString("sex"));
    s.setWeight(rs.getInt("weight"));
    s.setBri(rs.getDate("bri"));
    list.add(s);
   }
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   BaseDao.close(rs,stat, conn);
  }
  return list;
 }
 
_________________________________________________
 
package servlet;

import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import Dao.StudentDao;
public class QueryStudentServlet extends HttpServlet {
  @Override
  protected void service(HttpServletRequest req, HttpServletResponse arg1)
    throws ServletException, IOException {
    StudentDao sd=new StudentDao();
    List list= sd.queryStudentAll();
    req.setAttribute("list",list);
    req.getRequestDispatcher("index.jsp").forward(req, arg1);
  }
 
为什么报java.lang.NullPointerException,oracle数据库也没事,是哪里没配置好吗?
大佬帮我解决一下。
 
 
错误———错误————错误————————错误————————错误

type Exception report

message java.lang.NullPointerException

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: java.lang.NullPointerException org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:556) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:477) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339) javax.servlet.http.HttpServlet.service(HttpServlet.java:731) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

 

root cause

java.lang.NullPointerException org.apache.jsp.index_jsp._jspService(index_jsp.java:128) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) javax.servlet.http.HttpServlet.service(HttpServlet.java:731) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339) javax.servlet.http.HttpServlet.service(HttpServlet.java:731) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
 
 
寒兔的主页 寒兔 | 初学一级 | 园豆:183
提问于:2018-08-25 09:44

你倒是把异常信息贴出来啊,不贴出来怎么知道哪里有问题

青雉 6年前

@青雉: 我把错误放在题目下面了

寒兔 6年前
< >
分享
最佳答案
0

Object obj=request.getAttribute("list");
List<Student> stus=(List<Student>)obj;
for(Student s : stus){
%>
<TR>
<td><%=s.getSid()%></td>
<td><%=s.getSname()%></td>
<td><%=s.getSex()%></td>
<td><%=s.getWeight()%></td>
<td><%=s.getBri()%></td>
<td>
<a href="">删除</a>
<a href="">修改</a>
</td>
</TR>
<%
}
%>
检查一下这里是不是出了问题吧,在Dao层设置断点于 return list;处,先看看数据是否正常或得到,如果断点能走到return list;你就看看list里的值是否正常,如果正常那基本就能肯定是你JSP里那段代码出问题了。

奖励园豆:5
山抹微云君^^ | 菜鸟二级 |园豆:474 | 2018-08-27 09:18

我已经解决了,我运行servlet文件就行了

寒兔 | 园豆:183 (初学一级) | 2018-08-27 10:57
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册