下面是源码,后面我会说明问题:
//---------------------------------
//分页的配置
public class PageConf 
{
public static final int PAGE_SIZE_NORMAL = 15;  
}
//--------------------------------
//page类
public class Page implements Serializable {
private static final long serialVersionUID = 1L;
public static Page EMPTY_PAGE=new Page();
private int rowsPerPage = PageConf.PAGE_SIZE_NORMAL;   //页面大小,记录条数
private int start;                       //本页数据在数据库中的起始位置
private int curRowCount ,maxRowCount;    //本页包含的数据条数
private Vector data;          //当前页的数据
private int curPageNo;                   //当前页码
private int maxPageNo;                   //总页数


/**
/*默认构造方法,只构造空页
 * 
 *
 */
public Page()
{
this.init(0,0,0,PageConf.PAGE_SIZE_NORMAL,new Vector());

public Page(int start, int curRowCount , int maxRowCount, int rowsPerPage, Vector data)
{
this.init(start, curRowCount , maxRowCount, rowsPerPage,data);
}

 /** 分页数据初始方法,由子类调用
 * @param start 本页数据在数据库中的起始位置
 * @param curRowCount  本页包含的数据条数
 * @param maxRowCount 数据库中总记录条数
 * @param rowsPerPage 本页容量
 * @param data 本页包含的数据 
 */
 public void init(int start, int curRowCount , int maxRowCount, int rowsPerPage, Vector data)
 {   
this.curRowCount  =curRowCount ;
this.rowsPerPage = rowsPerPage;
this.start = start;
this.maxRowCount = maxRowCount;
this.data=data;

if (curRowCount >maxRowCount)  //本页包含记录数大于总的记录数
{                              
// throw new RuntimeException("记录条数大于总条数?!");
}

this.curPageNo = (start -1)/rowsPerPage +1;
this.maxPageNo = (maxRowCount + rowsPerPage -1) / rowsPerPage;

if (maxRowCount==0 && curRowCount ==0)
{
this.curPageNo = 1;
this.maxPageNo = 1;
}
//               System.out.println("Start Index to Page No: " + start + "-" + curPageNo);
   } 
//^^^^^^省略一些
public void countmaxPageNo() //根据总行数计算总页数  
{         
if (this.maxRowCount % this.rowsPerPage==0)
{
this.maxPageNo = this.maxRowCount/this.rowsPerPage;
}
else
{
this.maxPageNo = this.maxRowCount/this.rowsPerPage + 1;        
}
}  /**
 * getter and setter methods
 * @return
 */

public String toString()
{
return new String("maxRowCount:"+this.getMaxRowCount()+
" maxPageNo:"+this.getmaxPageNo()+
" CurPageNo:"+this.getCurPageNo());
}

}
//--------------------------
//管理page类
package com.data;import com.db.DBConnection;
import com.data.Page;
import com.data.PageConf;
import java.util.Vector;public class PageView {
private DBConnection dbc;
private Page page;
private Vector<Object[]> vctr;public String SQLS;//public Page getPage() {
return page;
}public PageView()
{
this.dbc=new DBConnection();
this.page=new Page();
}
/**
 * 列出指定页码的UserInfoBean的page对象
 * @param sqlCnst  查询条件
 * @param pageNo  要查询的页码
 * @param pageSize 页面大小,每页包含的记录数
 * @return
 */
public Page listUser(String sqlCnst,int pageNo,int pageSize)
{
StringBuffer sb=new StringBuffer();
sb.append("select count(*) from TestTable");
sb.append(sqlCnst);
this.dbc.executeQuery(sb.toString());
this.page.setMaxRowCount(this.dbc.rstGetInt(1));//设置page对象中的最大记录数
this.page.setRowsPerPage(pageSize);   //设置每页记录数目
this.page.setCurPageNo(pageNo);       ////设置当前页码
if(pageNo>this.page.getmaxPageNo()) 
{
return this.page.EMPTY_PAGE;
}
sb.delete(0, sb.length());
sb.append("select top ");
sb.append(PageConf.PAGE_SIZE_NORMAL);
sb.append(" * from TestTable where (ID > (select max(ID) from (select top ");
sb.append(PageConf.PAGE_SIZE_NORMAL*(pageNo-1));
sb.append(" ID from TestTable ");
sb.append(sqlCnst);
sb.append(" order by ID) as t)) order by ID ");
//下面的字符串我是我用替换上面sb产生的字符串的,结果也是打印不出
this.SQLS="SELECT TOP 20 * FROM TestTable WHERE ID > (SELECT MAX(id) FROM (SELECT TOP 200 id FROM TestTable ORDER BY id) AS T) ORDER BY ID"; 
this.dbc.executeQuery(SQLS);//sb.toString());
while(this.dbc.rstNext())
{
Object[] obj=new Object[5];
obj[0]=Integer.valueOf(this.dbc.rstGetInt("ID"));
obj[1]=this.dbc.rstGetString("FirstName");
obj[2]=this.dbc.rstGetString("LastName");
obj[3]=this.dbc.rstGetString("Country");
obj[4]=this.dbc.rstGetString("Note");
this.vctr.add(obj);
}
this.page.setData(this.vctr);
return this.page;
}
public String getSQLS()
{
return this.SQLS;
}}
//-----------------
//servlet中的代码
public void doPost(HttpServletRequest request, HttpServletResponse response) 
throws ServletException, IOException 
{
// TODO Auto-generated method stub
String userId=request.getParameter("userId");
response.setContentType("text/html;chaeset=gb2312");
response.setCharacterEncoding("gb2312");
String temp=null;
if(userId==null)   //
{
try
{
String jmpage=request.getParameter("page");
if(jmpage==null)
{
jmpage="1";
}
int pageNo;
try
{
pageNo=Integer.parseInt(jmpage);
}
catch(Exception e)
{
pageNo=1;
}
PageView pv=new PageView();
Page pg=pv.listUser("",pageNo,PageConf.PAGE_SIZE_NORMAL);
request.setAttribute("pagedata", pg);
//RequestDispatcher dis=request.getRequestDispatcher("/admin/viewuser.jsp?"+request.getQueryString());//?userId=-1&page="+jmpage");
//dis.forward(request, response);
//下面是我测试用的结果打印出来SQL 语句是null???
PrintWriter pw=response.getWriter();
pw.println("<h1>结果:</h1>"+"<br />");
pw.println("SQL Query String:  "+pv.getSQLS()+"<br />");
pw.print(pg.toString());
}
catch(Exception e)
{
System.out.println(e.toString());
PrintWriter pw=response.getWriter();
pw.println("<h1>产生异常!</h1>"+"<br />");
pw.println(temp);
pw.println("<h4>e.toString():</h4>"+e.toString()+"<br />");
pw.println("<h4>e.getMessage():</h4>"+e.getMessage()+"<br />");
pw.println("<hr />");
e.printStackTrace(pw);
}
}
else   //查看
{
PrintWriter pw=response.getWriter();
pw.println("<h1>反反复复反反复复!</h1>");
}
}
-------------------------------------
错误代码是:
产生异常!null
e.toString():
java.lang.NullPointerException
e.getMessage():
null
java.lang.NullPointerException at com.data.PageView.listUser(PageView.java:62) at com.util.ViewUserServlet.doPost(ViewUserServlet.java:55) at com.util.ViewUserServlet.doGet(ViewUserServlet.java:22) at javax.servlet.http.HttpServlet.service(HttpServlet.java:690) at javax.servlet.http.HttpServlet.service(HttpServlet.java:803) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188) at com.qxauc.authenticate.AdminAccessCtrlFilter.doFilter(AdminAccessCtrlFilter.java:48) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188) at

解决方案 »

  1.   

    java.lang.NullPointerException  at  com.data.PageView.listUser(PageView.java:62)  at
    这一行是:
                           obj[4]=this.dbc.rstGetString("Note");  
    //62                       this.vctr.add(obj);  
               }  
               this.page.setData(this.vctr);  
               return  this.page;
      

  2.   

    vctr 你只是声明,没有实力化.
    所以null
      

  3.   

    好像是那个问题呢
    不过又有个问题
    sb.append("select count(*) from TestTable");
    sb.append(sqlCnst);
    this.dbc.executeQuery(sb.toString());
    this.page.setMaxRowCount(this.dbc.rstGetInt(1));//设置page对象中的最大记录数
    这一段设置的maxRowCount的值为什么会是-1呢?
    明明有20000条数据的呢!
    我在查询分析器里测试了语句是没有问题的啊!
      

  4.   

    没有细看
    怎么会有Object[] obj=new Object[5];这种浪费资源的语句
    建议List或者Collection
      

  5.   

    csdn里搜索一下。有很多分页的解决方案.
      

  6.   

    一个sql语句就可搞定的东东,搞这么复杂
      

  7.   

    sb.append("select count(*) from TestTable");
    sb.append(sqlCnst);
    sqlCnst是什么东西?sb.toString()看看这个打印出来的sql语句对不对.
      

  8.   

    rs=stmt.executeQuery(strSql);
    CachedRowSetImpl crs = new CachedRowSetImpl();
    crs.setPageSize(Constants.pageSize);
    crs.populate(rs);
    rowCount = crs.size();..........
    crs.populate(rs, (curPage - 1) * crs.getPageSize() + 1);
    详细的你查一下帮助,祝你好运。
      

  9.   

    sb.append("select count(*) from TestTable");
    sb.append(sqlCnst);
    this.dbc.executeQuery(sb.toString());
    this.page.setMaxRowCount(this.dbc.rstGetInt(1));
    =============================================
    这里的sqlCnst是什么?
    还有这个方法
    this.dbc.executeQuery(sb.toString());赋值给resultset的操作没有问题吗?this.dbc.rstGetInt这个方法里面用到的rs跟你刚刚赋值的是同一个rs吗?
      

  10.   

    sqlCnst时sql语句中的查询条件
    dbc是一个怎么说呢封装了数据库操作的类
    他没执行一次查询以后rs都会变