java.lang.NullPointerException 怎么解决?

来源:百度知道 编辑:UC知道 时间:2024/07/05 02:59:29
请看代码:
<%!
private void tree(List<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("rootid"));
a.setIsleaf(rs.getInt("isleaf") == 0?true:false);
a.setPdate(rs.getTimestamp("pdate"));
a.setGrade(grade);
articles.add(a);
if(!a.isleaf()){
tree(articles,conn,a.getId(),grade+1);
}
}
}catch(SQLException e){
e.printStackTrace();
}
}
%>

<%
List<Article> articles = new ArrayList<Article>();
C

空指针异常,比如当rs返回null值的时候,你再调用他的方法就会报空指针异常。

另外你还在用MVC1模式写项目,还是尽快用struts+hibernate 构建吧,你会发现效率的提升,如同从老牛拉车过渡到磁悬浮列车。

rs 为空 执行SQL语句的时候返回的是一个空的 ResultSet
具体咋空就要看程序了。。