【问题标题】:Getting exception : java.lang.NullPointerException on my jsp page获取异常:我的 jsp 页面上的 java.lang.NullPointerException
【发布时间】:2023-04-10 19:32:01
【问题描述】:

我正在发布我的代码,其中我得到了 java.lang.NullPointerException 异常。

<%@ page contentType="text/html;charset=UTF-8" %>
<%@ page errorPage="myError.jsp?from=customers.jsp" %>
<%@ page import="java.sql.*" %>
<html>
<head>
<title>Insurance Quoting System</title>
</head>
<body>
<basefont face="Arial">
<!-- JSP Declarations -->
<%! ResultSet rs = null; %>

<!-- JSP Scriptlet -->
<%
try {
Class.forName("com.mysql.mysql.Driver");
Connection db = DriverManager.getConnection("jdbc:mysql://localhost:3306/quoting","root","root");
Statement s = db.createStatement();
rs = s.executeQuery("select * from customer");
}
catch (Exception e) {
// For now, just report the error to the system log
System.out.println(e.toString());
}
%>
<!-- Template text -->
<table width="550" border="0" align="center">
<tr>
<td bgcolor="#006633">
<div align="center">
<font size="6" color="#FFFFFF">
<b>Insurance Quoting System</b>
</font>
</div>
</td>
</tr>
<tr>
<td>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p align="center"><b>Customers</b></p>
<table width="290" border="0" align="center">
<%
try {
while (rs.next()) {
%>
<!-- JSP Expressions used within template text -->
<tr>
<td width="20"><%= rs.getInt(1) %></td>
<td width="70"><%= rs.getString(2) %></td>
<td width="70"><%= rs.getString(3) %></td>
<td width="40">
<a href="custMaint.jsp?id=<%= rs.getString(1) %>&action=edit">
edit
</a>
</td>
<td width="40">
<a href="custMaint.jsp?id=<%= rs.getString(1) %>&action=delete">
delete
</a>
</td>
<td width="40">
<a href="custMaint.jsp?id=<%= rs.getString(1) %>&action=newQuote">

new quote
</a>
</td>
</tr>
<%
}
}
catch (SQLException e) {
// For now, just report the error to the system log
System.out.println(e.toString());
}
%>
</table>
</td>
</tr>
<tr>
<td>
<p>&nbsp;</p>
<p align="center"><a href="custMaint.jsp?action=add">New Customer</a></p>
</td>
</tr>
</table>
</body>
</html>

我正在制作一个 jsp 页面,它在我的 jsp 页面上给出了一些错误 java.lang.NullPonterException,如果有人有,请告诉我 ans。

【问题讨论】:

  • 堆栈跟踪,谢谢。
  • 调试一下就知道问题出在哪里了。

标签: java jsp


【解决方案1】:

我能想到的唯一原因是您在第一个 try-catch 块中捕获异常,然后尝试使用 ResultSet 就好像它已被初始化,但它仍然为空,所以您正在获得 NPE。检查您的数据库操作是否正常工作。

【讨论】:

    猜你喜欢
    • 2013-01-03
    • 1970-01-01
    • 2014-05-03
    • 1970-01-01
    • 1970-01-01
    • 2015-09-19
    • 2015-03-06
    • 1970-01-01
    • 2014-10-26
    相关资源
    最近更新 更多