教程上的一个代码,rs.next()报java.lang.NullPointerException,DB是封装的类,我写程序测试了一下DB类和Article也能取到数,但是运行网页就报错,是不是递归写的有问题,求各位指教!
<%@page pageEncoding="GB18030"%>
<%@page import="java.sql.*,com.bjsxt.bbs.*,java.io.*,java.util.*" %>
<%!private void tree(Set<Article> articles, Connection conn, int id, int grade) {
{
String sql = "select * from article where pid = " + id;
Statement stmt = DB.createStmt(conn);
ResultSet rs = DB.executeQuery(stmt, sql);
try {
while (rs.next()) {
Article a = new Article();
a.setId(rs.getInt("ID"));
a.setPid(rs.getInt("pid"));
a.setRootId(rs.getInt("RootId"));
a.setTitle(rs.getString("Title"));
a.setLeaf(rs.getInt("isleaf") == 0 ? true : false);
a.setGrade(grade);
articles.add(a);
if (!a.isLeaf()) {
tree(articles, conn, a.getId(), grade + 1);
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}%>
<%
Set<Article> articles = new HashSet<Article>();
Connection conn = DB.getConn();
tree(articles, conn, 0, 0);
DB.close(conn);
%>
------解决方案--------------------
ResultSet rs = stmt.executeQuery(sql);