NullPointerException??马上结贴!!
提交页面的html文件如下:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>login</title>
<style type="text/css">
<!--
a:link,a:visited {text-decoration:none;color:#333333}
a.lb:hover{text-decoration:underline;color:white}
a:hover {text-decoration:underline;color:#003399}
a:active{text-decoration:none;color:black}
.cn0 {font-family: "宋体"; font-size: 12.8px}
.cn1 {font-family: "宋体"; font-size: 12.8px; line-height:150%}
.cn2 {font-family: "宋体"; font-size: 12.8px; line-height:150%; letter-spacing: 2px}
.cn3 {font-family: "宋体"; font-size: 12.8px; line-height:150%;color:#336699}
.cn4 {font-family: "宋体"; font-size: 12.8px; line-height:170%}
.cn5 {font-family: "宋体"; font-size: 12.8px; line-height:170%; letter-spacing: 2px}
.cn6 {font-family: "宋体"; font-size: 12.8px; line-height:170%;color:#336699}
.cnt0 {font-family: "宋体"; font-size: 14.5px}
.cnt1 {font-family: "宋体"; font-size: 14.5px;line-height:160%}
.cnt2 {font-family: "宋体"; font-size: 14.5px;line-height:160%; letter-spacing: 3px}
.cnt3 {font-family: "宋体"; font-size: 14.5px;line-height:160%;color:#336699}
.e0 {font-family: "Arial","tahoma"; font-size: 9pt}
.e1 {font-family: "Arial","tahoma"; font-size: 9pt;line-height:140%}
.e2 {font-family: "Arial","tahoma"; font-size: 14.5px;line-height:160%}
-->
</style>
</head>
<body bgcolor="#99CCCC">
<p align="center"> </p>
<p align="center"> </p>
<p align="center"><img src="images/duke2.gif"></p>
<p align="center"><font color="#000000"><b><font color="#666666">[登录三角地聊天室]</font></b></font></p>
<form method="POST" action="login.jsp">
<div align="center"><center>
<table border="0" width="270" height="245" cellpadding="2" cellspacing="3" bgcolor="339999">
<tr>
<td width="70">
<div align="center" class="cn0"><font color="#FFFFCC">[呢称]</font></div>
</td>
<td width="200" class="cn0">
<input type="text" name="username" size="15">
</td>
</tr>
<tr>
<td>
<div align="center" class="cn0"><font color="#FFFFCC">[用户名]</font></div>
</td>
<td class="cn0">
<input type="text" name="sex2" size="15">
</td>
</tr>
<tr>
<td>
<div align="center" class="cn0"><font color="#FFFFCC">[密码]</font></div>
</td>
<td class="cn0">
<Input type="password" name="sex" size="15">
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="我要聊天">
<input type="reset" value="不想聊了">
</td>
</tr>
</table>
</center></div>
</form>
</body>
</html>
jsp文件如下:
<!--this file are written by fancy-->
<html>
<head>
<title>
login the first chat room
</title>
</head>
<%@ page import="java.util.Vector" %>
<%@ page contentType="text/html;charset=gb2312" %>
<%@ page import="java.util.Hashtable" %>
<%@ page import="java.util.Date" %>
<%
byte[] username_p;
String username_t=request.getParameter("username");
username_p=username_t.getBytes("ISO8859-1");
String username=new String(username_p);
boolean boolcanlogin=true;
%>
<%
Vector temp=new Vector();
String tempuser;
//*****************************************************************************
temp=(Vector)getServletContext().getAttribute("chat1User");
//if (temp!=null){
for(int i=0;i<temp.size();i++)
{
tempuser=temp.elementAt(i).toString();
if(username.equals(tempuser))
{
boolcanlogin=false;
}
}
//}
//*****************************************************************************
if (username.equals(""))
{
boolcanlogin=false;
}
if (username.equals("每一个人"))
{
boolcanlogin=false;
}
if (username.equals("all"))
{
boolcanlogin=false;
}
if (username.length()>8)
{
boolcanlogin=false;
}
//*****************************************************************************
if(boolcanlogin==true)
{
temp.addElement(username); //如果检查通过则在用户向量中加入新的用户名
getServletContext().setAttribute("chat1User",temp);
session.putValue("username",username); //设置session记录用户名
//添加欢迎信息到消息向量里
Vector tempmsg=(Vector)getServletContext().getAttribute("chat1Msg");
tempmsg.addElement("<font size='2' color='red'>"+username+"来了["+request.getRemoteAddr()+"],大家欢迎!</font><br>");
getServletContext().setAttribute("chat1Msg",tempmsg);
//输出欢迎页面
out.println("<p align='center'><font color='#8080FF'>未名聊天室1</font></p>");
out.println("<hr width='80%' color='#0000FF'>");
out.println("<div align='center'><center>");
out.println("<table border='0' width='86%' height='233'>");
out.println("<tr>");
out.println("<td width='4%' height='23'><img src='new.gif' width='20' height='20'");
out.println("alt='new.gif (970 bytes)'></td>");
out.println("<td width='96%' height='23' bgcolor='#B5B5FF'>各位聊客,欢迎使用本聊天室,本聊天室支持人数无上限,每隔10秒刷新消息队列,每隔</td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td width='4%' height='13'></td>");
out.println("<td width='96%' height='13' bgcolor='#B5B5FF'>两分钟刷新用户列表,如果用户在四分钟内没有任何动作的话,那么此用户将被注销.</td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td width='4%' height='11'><img src='new.gif' width='20' height='20'");
out.println("alt='new.gif (970 bytes)'></td>");
out.println("<td width='96%' height='11' bgcolor='#CACAFF'>本聊天室支持34种动作,31种表情,悄悄话等功能,今后将加入图形化功能,及跳转功能,</td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td width='4%' height='7'></td>");
out.println("<td width='96%' height='7' bgcolor='#CACAFF'>新建聊天室等功能.</td>");
out.println(" </tr>");
out.println("<tr>");
out.println("<td width='4%' height='19'><img src='new.gif' width='20' height='20'");
out.println("alt='new.gif (970 bytes)'></td>");
out.println("<td width='96%' height='19' bgcolor='#D2D2FF'>如果你要离开本聊天室,请点击右边窗口内的"关闭".</td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td width='4%' height='20'><img src='new.gif' width='20' height='20'");
out.println("alt='new.gif (970 bytes)'></td>");
out.println("<td width='96%' height='20' bgcolor='#EEEEFF'>如果您有什么批评及建议,请到"三角地"发表文章.</td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td width='4%' height='22'><img src='new.gif' width='20' height='20'");
out.println("alt='new.gif (970 bytes)'></td>");
out.println("<td width='96%' height='22' bgcolor='#F4F4FF'>请不要在本聊天室内发表反动言论及对他人进行人身攻击.</td>");
out.println("</tr>");
out.println("</table>");
out.println("</center></div>");
out.println("<p align='center'><a href='main.html' target='_top'>请进</a></p>");
}
else
{
out.println("<center><font color='red' size='2'>用户名相同");
out.println("<a href='login.html'>重新申请ID</a></font></center>");
}
%>
<%
if (boolcanlogin==true)
{
//用一个哈希表来记录用户在聊天室呆的时间
Hashtable userLife=(Hashtable)getServletContext().getAttribute("userLife");
//用一个哈希表记录在聊用户的IP地址
Hashtable userIP=(Hashtable)getServletContext().getAttribute("userIP");
//取得用户登录时间
Date userTime=new Date();
//记录用户登录时间
userLife.put((String)session.getValue("username"),new Long(userTime.getTime()));
//记录用户IP地址
userIP.put((String)session.getValue("username"),request.getRemoteAddr());
getServletContext().setAttribute("userLife",userLife);
getServletContext().setAttribute("userIP",userIP);
}
%>
<body>
</body>
</html>
运行时产生NullPointerException错误。是为什么?大家指点一下!!
问题点数:50、回复次数:4Top
1 楼jan4984(Janny)回复于 2003-09-04 22:56:16 得分 15
调用了空对象的方法吧。
把具体的出错报告给出来才知道。Top
2 楼squallzeng(小呆呆)回复于 2003-09-05 08:49:15 得分 15
告诉你这是一个空的过程,为什么这么说呢,就是说你的一些要传的参数没有传。也说是说你的传参数这一块没有通过!!!!!具体要看你的错误报告是什么才能定!!!!Top
3 楼susan1212()回复于 2003-09-05 08:57:38 得分 5
可能有某个对象没有初始化!!Top
4 楼fft123(专杀各类日本畜生)回复于 2003-09-05 09:02:55 得分 15
temp=(Vector)getServletContext().getAttribute("chat1User");
//if (temp!=null){
for(int i=0;i<temp.size();i++)
{
tempuser=temp.elementAt(i).toString();
if(username.equals(tempuser))
{
boolcanlogin=false;
}
}
//}
这里有判空的语句的,为什么被注释掉了?
打开注释再试试
NullPointerException这个错误就是说当你程序中某个变量值为null时你调用了它的方法或属性Top



