有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

获取异常:java。我的jsp页面上的lang.NullPointerException

我发布了我的代码,在其中我得到了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页面,它给出了一些java错误。我的jsp页面上的lang.NullPonterException请告诉我ans是否有


共 (1) 个答案

  1. # 1 楼答案

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