本地创建一个Oracle数据库,里面建立一个表格person,有一系列的数据。现在需要用JDBC+Servlet把数据库连接到JSP上让别的用户自己往数据库里增删改查,并且做到分页等等等等小功能。
代码基本上写好了,感觉code逻辑也没什么错,JSP上的格式引用应该也对,但是每次跑起来都会说我有空指针错误!每一页JSP都有空指针,排查以后大概指向了这一块 //获得分页显示的子集 的代码上(因为每一个JSP的报错都是因为用到了subResult这个指针)。现在想把这一块的JAVA CODE贴出来,求大佬帮忙看看到底哪个出现空指针了。
public void difpage(HttpServletRequest request,HttpServletResponse response) throws ClassNotFoundException,SQLException,
ServletException, IOException{
//String id = request.getParameter("id");
// String name = request.getParameter("Lastname");
ArrayList<data>result = select("","");
page pager = new page();
pager = setpage(request,response);
List<data> subResult = null;
int currentRecord=pager.getCurrentRecord();
if(currentRecord==0){
if(pager.getTotalRecord()<8){
subResult=(List<data>) result.subList(0,pager.getTotalRecord());
}
else {
subResult=(List<data>)result.subList(0, pager.getPageSize());
}
}
else if(pager.getCurrentRecord()+pager.getPageSize()<result.size()){
subResult=(List<data>)result.subList(pager.getCurrentRecord(), pager.getCurrentRecord()+pager.getPageSize());
}
else{
subResult=(List<data>)result.subList(pager.getCurrentRecord(), result.size());
}
request.setAttribute("pager", pager);
request.setAttribute("subResult", subResult);
request.getRequestDispatcher("layout.jsp").forward(request, response);
}
3 个解决方案
#1
自己看log,或调试呀,直接就到错的那行了,看也只能猜
#2
public void difpage(HttpServletRequest request,HttpServletResponse response) throws ClassNotFoundException,SQLException,
ServletException, IOException{
//String id = request.getParameter("id");
// String name = request.getParameter("Lastname");
ArrayList<data>result = select("","");
page pager = new page();
pager = setpage(request,response);
List<data> subResult = null;
int currentRecord=pager.getCurrentRecord();
if(currentRecord==0){
if(pager.getTotalRecord()<8){
subResult=(List<data>) result.subList(0,pager.getTotalRecord());
}
else {
subResult=(List<data>)result.subList(0, pager.getPageSize());
}
}
else if(pager.getCurrentRecord()+pager.getPageSize()<result.size()){
subResult=(List<data>)result.subList(pager.getCurrentRecord(), pager.getCurrentRecord()+pager.getPageSize());
}
else{
subResult=(List<data>)result.subList(pager.getCurrentRecord(), result.size());
}
request.setAttribute("pager", pager);
request.setAttribute("subResult", subResult);
request.getRequestDispatcher("layout.jsp").forward(request, response);
}
你重点看看你这几个if判断 我怀疑
ArrayList<data>result = select("",""); // 这里可能直接返回空
或者if判断那里导致subResult 根本就没有被赋值
#3
你在你贴的代码里打个断点,debug下不就知道哪一行报错了吗?
#1
自己看log,或调试呀,直接就到错的那行了,看也只能猜
#2
public void difpage(HttpServletRequest request,HttpServletResponse response) throws ClassNotFoundException,SQLException,
ServletException, IOException{
//String id = request.getParameter("id");
// String name = request.getParameter("Lastname");
ArrayList<data>result = select("","");
page pager = new page();
pager = setpage(request,response);
List<data> subResult = null;
int currentRecord=pager.getCurrentRecord();
if(currentRecord==0){
if(pager.getTotalRecord()<8){
subResult=(List<data>) result.subList(0,pager.getTotalRecord());
}
else {
subResult=(List<data>)result.subList(0, pager.getPageSize());
}
}
else if(pager.getCurrentRecord()+pager.getPageSize()<result.size()){
subResult=(List<data>)result.subList(pager.getCurrentRecord(), pager.getCurrentRecord()+pager.getPageSize());
}
else{
subResult=(List<data>)result.subList(pager.getCurrentRecord(), result.size());
}
request.setAttribute("pager", pager);
request.setAttribute("subResult", subResult);
request.getRequestDispatcher("layout.jsp").forward(request, response);
}
你重点看看你这几个if判断 我怀疑
ArrayList<data>result = select("",""); // 这里可能直接返回空
或者if判断那里导致subResult 根本就没有被赋值
#3
你在你贴的代码里打个断点,debug下不就知道哪一行报错了吗?