这个程序怎么会抛nullpointerException呢?各位大侠帮我看看!

zhuhezan 2010-11-22 10:58:12
<%@ page pageEncoding="gb18030" %>
<%@ page import="java.sql.*, com.bjsxt.bbs.*, java.util.*"%>

<%!
private void tree(Set<Article> articles, Connection conn, int id, int grade) {
String sql = "select * from article where pid = " + id;
System.out.println(sql);
Statement stmt = DB.createStmt(conn);
System.out.println(conn);
System.out.println(stmt);
ResultSet rs = DB.executeQuery(stmt, sql);

System.out.println(rs);
try {
while(rs.next()) {
Article a = new Article();
a.setId(rs.getInt("id"));
a.setPid(rs.getInt("pid"));
a.setRootid(rs.getInt("rootid"));
a.setGrade(grade);
a.setTitle(rs.getString("title"));
a.setLeaf(rs.getInt("isleaf") == 0 ? true : false);
a.setPdate(rs.getTimestamp("pdate"));
articles.add(a);
if (!a.isLeaf()) {
tree(articles, conn, a.getId(), grade+1);
}
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DB.close(stmt);
DB.close(rs);
}
}
%>

<%
Set<Article> articles = new HashSet<Article>();
Connection conn = DB.getConn();
tree(articles, conn, 0, 0);
DB.close(conn);
%>

这是我的写的JSP的部分代码, 这程序总是在while(rs.next()) 行抛nullpointerException 错;
这是为什么啊?


DB是为连接数据库而封装的一个类, Article 也是封装的一个类;
Article.java 代码如下:
package com.bjsxt.bbs;
import java.util.*;

public class Article {

private int id;
private int pid;
private int rootid;
private int grade;
private String title;
private String cont;
private Date pdate;
private boolean isLeaf;

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public int getRootid() {
return rootid;
}
public void setRootid(int rootid) {
this.rootid = rootid;
}

public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getCont() {
return cont;
}
public void setCont(String cont) {
this.cont = cont;
}
public Date getPdate() {
return pdate;
}
public void setPdate(Date pdate) {
this.pdate = pdate;
}
public boolean isLeaf() {
return isLeaf;
}
public void setLeaf(boolean isLeaf) {
this.isLeaf = isLeaf;
}
}
...全文
137 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
e3e4p 2010-11-23
  • 打赏
  • 举报
回复
结果集为空,插进去几条数据吧,如果只是防止报错,在前面加上,if(rs!= null),然后再遍历就好了
zhuhezan 2010-11-23
  • 打赏
  • 举报
回复
数据库里面没有数据?
xiaoair 2010-11-23
  • 打赏
  • 举报
回复
往数据库里插几条数据试试吧
bb12152205gg 2010-11-23
  • 打赏
  • 举报
回复
数据库没连上
njpingwen 2010-11-23
  • 打赏
  • 举报
回复
SQL打印出来的语句执行下,看看有没有数据

建议jsp页面中不要写java代码
Jlins 2010-11-23
  • 打赏
  • 举报
回复
。[Quote=引用 2 楼 soli11722984 的回复:]

把DB代码贴出来再说吧
[/Quote]
meteor1015 2010-11-23
  • 打赏
  • 举报
回复
rs 没有拿到

试试这样可以否

ResultSet rs = stmt.executeQuery(sql);
soli11722984 2010-11-23
  • 打赏
  • 举报
回复
把DB代码贴出来再说吧
leehuat 2010-11-23
  • 打赏
  • 举报
回复

数据库里面没有数据?

81,090

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧