回 帖 发 新 帖 刷新版面

主题:空指针错误:java.lang.NullPointerException

加红色部分后显示空指针错误:java.lang.NullPointerException.请问是为什么?代码如下:
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%@ include file="incoming/common.jsp" %>
<%@ include file="incoming/conndb.jsp" %>
<%@ include file="top.jsp" %>
<link href="incoming/css.css" rel="stylesheet" type="text/css">
<title>留言本</title>
<table align="center">
<tr>
<td><a href="login.jsp">用户登录</a>&nbsp;&nbsp;<a href="checkLogin.jsp?action=logout">退出登录</a>&nbsp;&nbsp;<a href="register.jsp">注册新用户</a></td>
<td>
          [color=FF0000]<%  
              String username=(String)session.getAttribute("username1"); 
              if(username.equals(null)){
                 out.println("未登录");
                 }
              else{
                 out.println("已登录");
                 }
            %>[/color]
</td>
</tr>
</table>
<%
try{
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rs=stmt.executeQuery("select * from bookinfo a,userinfo b where a.USERNAME=b.USERID order by a.ID desc");
int i=1;
int pagesize=8;
int pagecount;
int rowcount;
int gopage;
rs.last(); //将指针移动到表中的记录的最后面
rowcount = rs.getRow();//统计出一共有多少个记录
pagecount=(rowcount%pagesize==0)?(rowcount/pagesize):((rowcount/pagesize)+1);//得到需要显示的总页数
if(request.getParameter("page")!=null){//假如接收的页面数不为空
gopage = java.lang.Integer.parseInt(request.getParameter("page"));//就把接收的页面数pagg赋值给gopage
}else{//否则
gopage=1;//gopage为1,即显示第一页
}
rs.absolute((gopage-1)*pagesize+1);//将指针定位到哪一条记录

while(i<=pagesize && !rs.isAfterLast()){
Object id=rs.getObject("ID");
String isolate=rs.getString("ISOLATE");
String y="Y";
%>
<table width="771" border="0" align="center" cellspacing="1" bgcolor="#666666">
  <tr>
    <td width="200" height="178" bgcolor="#FFFFFF"><table width="100%" border="0" cellspacing="0">
      <tr>
        <td height="82"align="center"><img src=<%out.print(rs.getString("IMAGE"));%>>        </td>
      </tr>
      <tr>
        <td height="25"><div align="center">
        <%
        out.print(rs.getString("NAME")+"&nbsp;[<font color='#6666ff'>"+rs.getString("SEX")+"</font>]");
        %> 
        </div></td>
      </tr>
      <tr>
        <td height="25"><div align="center">
         <%
        out.print(rs.getString("TIME"));
        %> 
        </div></td>
      </tr>
      <tr>
        <td height="25"><div align="center"><img src="images/email.gif" width="60" height="17">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <img src="images/QQ.gif" width="60" height="17" alt="点击与对方建立QQ临时会话"border=0/></a></div></td>
      </tr>
      
    </table></td>
    <td bgcolor="#FFFFFF"><div align="center">
      <br />
      <table width="90%" height="20" border="0" cellspacing="1" bgcolor="#CCCCCC">
        <tr>
          <td bgcolor="#FFFFFF"><div align="left">主题:
           <%=rs.getString("TITLE")%>          
          </div></td>
        </tr>
      </table>
      <br />
      <table width="90%" height="127" border="0" cellspacing="1" bgcolor="#CCCCCC">
        <tr>
          <td bgcolor="#FFFFFF">
            <div align="left"> 
            <%
              if(isolate.equals(y)){
                 out.println("该留言已被屏蔽");
                 }
              else{
                 out.println(rs.getString("CONTENT"));
                 }
            %>
            </div></td>
        </tr>
      </table>
      <br />
      <table width="90%" height="20" border="0" cellspacing="1" bgcolor="#CCCCCC">
        <tr>
          <td bgcolor="#FFFFFF"><table width="100%" border="0" cellspacing="0">
            <tr>
              <td width="78%"><span class="STYLE2"></span></td>
              <td width="4%"></td>
              <td width="18%" class="STYLE3"></td>
            </tr>
          </table>          </td>
        </tr>
      </table>
      <br>
    </div></td>
  </tr>
</table><br>
<%
rs.next();
i++;
}
    rs.close();
    stmt.close();                        
%>
<table width="771" height="22" border="0" align="center" cellspacing="1" bgcolor="#999999">
  <tr>
    <td height="20" align="center" bgcolor="#CCCCCC" style="WORD-BREAK: break-all; WORD-WRAP: break-word">
共<%=pagecount%>页&nbsp;&nbsp;当前第<%=gopage%>页
      <% 
for(int j=1;j<=pagecount;j++){
out.print("&nbsp;&nbsp;<a href='index.jsp?page="+j+"'>"+j+"</a>");
}
}catch(Exception e){
out.print("<center>错误页码或无记录</center>");

}
%>&nbsp;&nbsp;&nbsp;&nbsp; 
      <div id="Layer1">
  <form id="form1" name="form1" method="post" action="index.jsp" >
        <label>
        <input name="page" type="text" class="dl2" id="page" size="3" />
        </label>
                    <label>
                    <input name="Submit" type="submit" class="dl2" value="GO" />
              </label>
</form>  </div>
</td>
  </tr>
</table>
[color=000000][/color]

回复列表 (共2个回复)

沙发

null值哪可用equals比较,空对象啊
 用 ==

板凳



 String username=(String)session.getAttribute("username1"); 

这句话可能得到空值,一般情况在你第一次登陆的时候,所以下面的代码应该改成如下:


    <%  
              String username=(String)session.getAttribute("username1"); 
              if(username==null){
                 out.println("未登录");
                 }
              else{
                 out.println("已登录");
                 }
            %>

我来回复

您尚未登录,请登录后再回复。点此登录或注册