当前位置: 代码迷 >> Java Web开发 >> 论坛分页功能,提示空指针,
  详细解决方案

论坛分页功能,提示空指针,

热度:2053   发布时间:2013-02-25 21:14:27.0
论坛分页功能,提示空指针,求助!!~~~~
Java code
<%    final int PAGE_SIZE = 4;    int pageNo = 1;    String strPageNo = request.getParameter("pageNo");    if (strPageNo != null && !strPageNo.trim().equals("")) {        try {            pageNo = Integer.parseInt(strPageNo);        } catch (NumberFormatException e) {            pageNo = 1;        }    }    if (pageNo <= 0)        pageNo = 1;    int totalPages = 0;    List<Article> articles = new ArrayList<Article>();    Connection conn = DB.getConn();    Statement stmtCount = DB.createStmt(conn);    ResultSet rsCount = DB.executeQuery(stmtCount,            "select count(*) from article where pid = 0");    rsCount.next();    int totalRecords = rsCount.getInt(1);    totalPages = (totalRecords + PAGE_SIZE - 1) / PAGE_SIZE;    if (pageNo > totalPages)        pageNo = totalPages;    Statement stmt = DB.createStmt(conn);    int startPos = (pageNo - 1) * PAGE_SIZE;    String sql = "select * from article where pid = 0 order by pdate desc limit "            + startPos + "," + PAGE_SIZE;    //System.out.println(sql);    ResultSet rs = DB.executeQuery(stmt, sql);//System.out.println(rs);这里rs输出的是空值,不知道是哪里出错了    while (rs.next()) {        Article a = new Article();        a.initFromRs(rs);        articles.add(a);    }    DB.close(rsCount);    DB.close(stmtCount);    DB.close(rs);    DB.close(stmt);    DB.close(conn);%>


------解决方案--------------------------------------------------------
自己好好检查,你这个真叫人无法下手
------解决方案--------------------------------------------------------
rsCount有值没有 如果rsCount有值 那应该是下面的查询或者其他错误 你在看看
------解决方案--------------------------------------------------------
你的这个 DB 是从哪里冒出来的?
------解决方案--------------------------------------------------------
DB 变量没有声明吧 你仔细看看
------解决方案--------------------------------------------------------
sql语句赋值到库里执行一下,看查出来的东西是不是空的,
------解决方案--------------------------------------------------------
像六楼说的去测一下 应改马上可以找到错误原因吧。。
------解决方案--------------------------------------------------------
如6L所说,先测试sql语句是否正确。

另外,操作数据库要使用try-catch,即使是jsp,也要保持这个习惯。


------解决方案--------------------------------------------------------
探讨

引用:

你的这个 DB 是从哪里冒出来的?


<%@page pageEncoding="UTF-8"%>
<%@page import="java.sql.*,bbs.*,java.util.*"%>
bbs这个包里的..

------解决方案--------------------------------------------------------
Java code
String sql = "select * from article where pid = 0 order by pdate desc limit "            + startPos + "," + PAGE_SIZE;
------解决方案--------------------------------------------------------
调试。。调试。。一调试就知道哪null指针了。。
  相关解决方案