Javaweb项目获取信息是如果先登录管理员查看用户信息就报空指针异常

Javaweb项目获取信息是如果先登录管理员查看用户信息就报空指针异常,如果先登其他用户再登管理员查看信息则只显示之前登录的用户信息
public String userlist(){
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
Students students = (Students)session.getAttribute("students");
String username = request.getParameter("username");
StringBuffer sb = new StringBuffer();
sb.append(" where ");
if(username !=null &&!"".equals(username)){
sb.append(" username like '%"+username+"%' ");

        request.setAttribute("username", username);
    }
    if(students.getRole()==2){
         sb.append(" stuid="+students.getStuid()+" and deletestatus=0 order by id desc ");//降序查询,ASC
        }
    if(students.getRole()==1){
        sb.append(" role=2 and deletestatus=0 order by id desc ");//降序查询,ASC
            }
    if(students.getRole()==3){
         sb.append(" role=2 and deletestatus=0 order by id desc ");
        }
    String where = sb.toString();
    int currentpage = 1;
    int pagesize = 10;
    if(request.getParameter("pagenum") != null){
        currentpage = Integer.parseInt(request.getParameter("pagenum"));
    }
    long total = studentsDao.selectBeanCount(where.replaceAll("order by id desc", ""));
    List<Students> list = studentsDao.selectBeanList((currentpage-1)*pagesize, pagesize, where);
    request.setAttribute("list", list);
    String pagerinfo = Pager.getPagerNormal((int)total, pagesize, currentpage, "method!userlist", "共有"+total+"条记录");
    request.setAttribute("pagerinfo", pagerinfo);

2个回答

报空指针if(students.getRole()==1){
有没有其他的方法替换一下

定位下空指针的位置,哪个对象没有判断空就直接调用方法。断点调试下,这种错误还是很容易解决的。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐