恰北北& 2023-10-20 19:59 采纳率: 0%
浏览 7

nullpointer报错空指针,代码没错,如何解决?(语言-java)

为什么一直报错空指针啊,有没有人可以帮我解决一下,花了十几个小时找不出错误,我寻思代码真的没错呀,谢谢
用servlet先访问,然后跳转,一直报404的错误

img

img

package servlets;

import  java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class StudentDao extends HttpServlet{
    public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
        Connection conn=null;
        String url="jdbc:mysql://localhost:3306/school?useSSL = false&serverTimezone = CST&characterEncoding=UTF-8";
        String user="root";
        String password="102245";
        ArrayList<String> STUNO=new ArrayList<>();
        ArrayList<String> STUNAME=new ArrayList<>();

        try {//连接数据库驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
        }
        catch(Exception e){
            System.out.println(e);
        }
        try {
            conn=DriverManager.getConnection(url,user,password);
            //运行SQL语句
            String sql="SELECT STUNO,STUNAME from t_student";
            Statement stat= conn.createStatement();
            ResultSet rs= stat.executeQuery(sql);
            while (rs.next()){
                STUNO.add(rs.getString("STUNO"));
                STUNAME.add(rs.getString("STUNAME"));
            }
            rs.close();
            stat.close();
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
           try {//关闭连接
               if (conn!=null){
                   conn.close();
                   conn=null;
               }
           }catch (Exception ex){
           }
        }
        request.setAttribute("STUNO",STUNO);
        request.setAttribute("STUNAME",STUNAME);
        String msg="lz";
        request.setAttribute("msg",msg);
        ServletContext application=this.getServletContext();
        RequestDispatcher rd=application.getRequestDispatcher("/index.jsp");
        rd.forward(request,response);
    }
}


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <servlet>
        <servlet-name>studentDao</servlet-name>
        <servlet-class>servlets.StudentDao</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>studentDao</servlet-name>
        <url-pattern>/StudentDao</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

</web-app>

<%--
  Created by IntelliJ IDEA.
  User: 86157
  Date: 2023/10/19
  Time: 8:33
  To change this template use File | Settings | File Templates.
--%>
import java.sql.*;
<%@ page language="java" pageEncoding="gb2312" %>
<%@ page import="java.util.ArrayList" %>
<html>
<head>
    <title>$Title$</title>
</head>

<body>
<table border="2">
    <tr>
        <td>学号</td>
        <td>姓名</td>
    </tr>
    <%
        String msg = (String) request.getAttribute("msg");
        ArrayList<String> STUNO = (ArrayList) request.getAttribute("STUNO");
        ArrayList<String> STUNAME = (ArrayList) request.getAttribute("StuNAME");
        System.out.println(msg);
        for(int i= 0 ; i< STUNO.size();i++){
            String Stuno = (String)STUNO.get(i);
            String Stuname = (String)STUNAME.get(i);
    %>
    <tr>
        <td><%=Stuno%></td>
        <td><%=Stuname%></td>
    </tr>
    <% } %>
</table>
</body>
</html>




  • 写回答

4条回答 默认 最新

  • micthis 2023-10-20 20:13
    关注

    STuNAME一个u大写一个小写,设置时大写获取时小写。

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 10月20日

悬赏问题

  • ¥15 三极管电路求解,已知电阻电压和三级关放大倍数
  • ¥15 ADS时域 连续相位观察方法
  • ¥15 Opencv配置出错
  • ¥15 模电中二极管,三极管和电容的应用
  • ¥15 关于模型导入UNITY的.FBX: Check external application preferences.警告。
  • ¥15 气象网格数据与卫星轨道数据如何匹配
  • ¥100 java ee ssm项目 悬赏,感兴趣直接联系我
  • ¥15 微软账户问题不小心注销了好像
  • ¥15 x264库中预测模式字IPM、运动向量差MVD、量化后的DCT系数的位置
  • ¥15 curl 命令调用正常,程序调用报 java.net.ConnectException: connection refused