java.lang.NullPointerException
在网上找了很久,也有很多相关的贴。大都多说是引用的对象为空,但在程序里我测试过了。rs对象并不为空。但是每当调用rs.getxx..(String ....)方法是就出现java.lang.NullPointerException
异常。也有人说是数据库里不存在root字段,但在我的数据库里我检查过好多次了。现在真不知道如何是好了。我也怀疑过是不是rs的作用域问题。但还是没什么结果。
希望高手能帮帮忙。在这里谢谢了。
甚至我怀疑过是不是没一个Statement职能唯一返回一个RsRsult对象。定义了两个Staement.但是,郁闷的是当第二次调用get.xxx()方法时又出现java.lang.NullPointerException.哎。。。。
<%--2008.10.1 ZiGong--%>
<%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%>
<%@ page import = "java.sql.*" %>
<%@ page import = "java.lang.*" %>
<%---------------------------method delete-----------------------------%>
<%!private void delete(Connection conn, int id) {
Statement stat = null;
ResultSet rs = null;
int deleteid = 0;
try {
stat = conn.createStatement();
rs = stat.executeQuery("select * from bbsrecord where sid=" + id);
while (rs.next()) {
deleteid = rs.getInt("id");
if (rs.getInt("leaf") == 0) {
stat.execute("delete from bbsrecord where id =" + deleteid);
delete(conn, deleteid);
} else {
stat.execute("delete from bbsrecord where id =" + deleteid);
}
}
} catch (SQLException e) {
System.out.println("SQL erro!");
} finally {
try {
rs.close();
stat.close();
} catch (SQLException e) {
System.out.println(":sqlexception:can not close");
}
}
}
%>
<%--------------------------the delete.jsp'start------------------------%>
<%
int id = Integer.parseInt(request.getParameter("id"));
int sid = 0;
Class.forName("java.sql.Driver");
Connection conn = null;
Statement stat = null;
String urlstring = "jdbc:mysql://127.0.0.1:3306/test?user=root&password=159753";
conn = DriverManager.getConnection(urlstring);
stat = conn.createStatement();
try {
ResultSet rs = stat.executeQuery("select * from BbsRecord where id=" + id);
rs.next();
sid = rs.getInt("sid");
ResultSet rss = stat.executeQuery("select * from bbsrecord where sid=" + sid);
rss.last();
if (rss.getRow() == 1) {
if (rs.getInt("root") == 0) { //错误报告指示的是这里,但我不理解,也找不到原因。
stat.execute("update bbsrecord set leaf = 1 where id=" + sid);
stat.execute("delete from bbsrecord where id =" + id);
if (rs.getInt("leaf") == 0) {
delete(conn,id);
}
}
} else {
stat.execute("delete from bbsrecord where id =" + id);
if (rs.getInt("leaf") == 0) {
delete(conn, id);
}
}
rs.close();
rss.close();
} catch (SQLException e) {
System.out.println("sql异常");
} finally {
stat.close();
conn.close();
}
%>
<%----------------------------- html ---------------------------------%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>数据的树型展现</title>
</head>
<body>
<center>
<h2><%="deleting record is succeed"%>
<%//response.sendRedirect("showtree.jsp");%></h2>
</center>
</body>
</html>
错误报告
HTTP Status 500 -
type Exception report
message
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:522)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:416)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
root cause
java.lang.NullPointerException
com.mysql.jdbc.ResultSetImpl.findColumn(ResultSetImpl.java:1059)
com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2719)
org.apache.jsp.delete_jsp._jspService(delete_jsp.java:110)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.16 logs.