gpt4 book ai didi

JSP中调用方法时出现Java NullPointerException

转载 作者:行者123 更新时间:2023-12-01 16:53:43 28 4
gpt4 key购买 nike

我有一个 Java 代码,它将 CSV 内的数据发送到数据库。当我运行 main java 方法时,这工作得很好,但是每当我加载 jsp 时,我都会收到此错误:

Type Exception Report

Message java.lang.NullPointerException

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

Exception

org.apache.jasper.JasperException: java.lang.NullPointerException
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:517)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Root Cause

java.lang.NullPointerException
org.apache.jsp.csv_jsp.method(csv_jsp.java:85)
org.apache.jsp.csv_jsp._jspService(csv_jsp.java:211)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:477)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Note The full stack trace of the root cause is available in the server logs.

这是我的 JSP:

<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.io.BufferedReader" %>
<%@ page import="java.io.FileReader" %>
<%@ page import="java.io.IOException" %>
<%@ page import="java.sql.SQLException" %>
<html>
<head>
<title>JSP</title>
</head>
<body>

<<input type="submit" onclick="<%method();%>"/>

<%!
static void method (){

String jdbcURL = "jdbc:mysql://localhost:3306/testDb";
String username = "root";
String password = "1234";

String csvFilePath = "C:/Users/Name/Desktop/Attendance.csv";

int batchSize = 20;

Connection connection = null;

try {

connection = DriverManager.getConnection(jdbcURL, username, password);
connection.setAutoCommit(false);

String sql = "INSERT INTO csv (name, `in-time`, `out-time`) VALUES (?, ?, ?)";
PreparedStatement statement = connection.prepareStatement(sql);

BufferedReader lineReader = new BufferedReader(new FileReader(csvFilePath));
String lineText = null;

int count = 0;

lineReader.readLine(); // skip header line

while ((lineText = lineReader.readLine()) != null) {
String[] data = lineText.split(",");
String name = data[0];
String inTime = data[1];
String outTime = data[2];

statement.setString(1, name);
statement.setString(2, inTime);
statement.setString(3, outTime);

statement.addBatch();

if (count % batchSize == 0) {
statement.executeBatch();
}
}

lineReader.close();

// execute the remaining queries
statement.executeBatch();

connection.commit();
connection.close();

} catch (IOException ex) {
System.err.println(ex);
} catch (SQLException ex) {
ex.printStackTrace();

try {
connection.rollback();
} catch (SQLException e) {
e.printStackTrace();
}
}

}
%>
</body>
</html>

Intellij 在回滚附近发送了有关发生这种情况的提示,但是当我删除它时,什么也没有发生。我得到了一个空白页,数据也没有进入数据库。谁能帮忙解决这个问题吗?

最佳答案

几件事:

  • 尝试进行一些空检查。
  • 你正在做这个模 - “count % batchSize == 0”,我认为应该是batchSize % count?
  • count 最初为 0,并且永远不会增加。

关于JSP中调用方法时出现Java NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61631696/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com