关于 ArrayList的一个问题

qindwt1 2010-05-22 12:17:07
今天我要疯的了,调了几个小时的程序始终无法解决一个 ArrayList.add()的一个问题! 给大家看下代码这是一个通过验证用户名邮箱找回密码的action。希望能帮我解决这个问题。在这先谢谢了。课程设计马上就要交了,都快疯的了。。。
package com.chat.struts.action;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import com.chat.DAO.IUserInfoDAO;
import com.chat.listener.OnLineListener;
import com.chat.struts.form.GetUserForm;
import com.chat.util.SendMail;
import com.chat.util.MD5Code;
public class GetUserAction extends DispatchAction{
IUserInfoDAO userInfoDAO;

public IUserInfoDAO getUserInfoDAO() {
return userInfoDAO;
}

public void setUserInfoDAO(IUserInfoDAO userInfoDAO) {
this.userInfoDAO = userInfoDAO;
}
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
GetUserForm gf = (GetUserForm) form;// TODO Auto-generated method stub
String username = gf.getUsername();
String email = gf.getEmail();
boolean IsExist = userInfoDAO.getUser(username, email);//查询用户名及相应的邮箱是否存在
if (IsExist) {
MD5Code md5=new MD5Code();
username=md5.getMD5ofStr(username+"chat");//加密用户名
int r=(int)(Math.random()*1000000)+1000000;//产生随即验证数
String random=String.valueOf(r);
random=md5.getMD5ofStr(random);
OnLineListener ol=new OnLineListener();
HashMap map=new HashMap();
map.put("username", username);
map.put("random", random);
ArrayList list=new ArrayList();
list=(ArrayList)ol.getApplication().getAttribute("check_uri");
list.add(map);//这一句出错 不管是 map 形式 还是 一个自定义的字符串都会报错 java.lang.NullPointerException
ol.getApplication().setAttribute("check_uri",list);
ArrayList ll=(ArrayList)ol.getApplication().getAttribute("check_uri");
SendMail sm=new SendMail();//更加奇怪的是 这一句也会报错。其存在构造方法 使用junit4测试通过。在别的类中也通过。
String qq[]={"qq","qq1"};
sm.send(qq);
String e[]=email.split("@");
String e_uri="http://mail."+e[1];
try {
response.sendRedirect(e_uri);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} else {
request.getSession().setAttribute("ErrorMessage",
"你输入的用户名和注册邮箱不匹配!请你重新输入");
return mapping.findForward("Fail");
}
return null;
}


}
...全文
135 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
piaolankeke 2010-05-23
  • 打赏
  • 举报
回复
好长的程序,也不排个版
chenliuyang 2010-05-23
  • 打赏
  • 举报
回复
等号少个=
chenliuyang 2010-05-23
  • 打赏
  • 举报
回复
List list=(List)ol.getApplication().getAttribute("check_uri");
if(list=null){
list=new ArrayList();
ol.getApplication().setAttribute("check_uri",list);
}
zhoutaibo 2010-05-22
  • 打赏
  • 举报
回复
2楼的一、很正确,转换用list=(List)ol.getApplication().getAttribute("check_uri");
qindwt1 2010-05-22
  • 打赏
  • 举报
回复
就是让我奇怪的是,没有getAttribute()这局 直接list.add()都报错让我郁闷的。
ditouye 2010-05-22
  • 打赏
  • 举报
回复
LZ,确定ol.getApplication().getAttribute("check_uri");这里面有值?
yudongming 2010-05-22
  • 打赏
  • 举报
回复
用断点的形式调式一下,进入到里面看一下list是否返回NULL.
list=(ArrayList)ol.getApplication().getAttribute("check_uri");
amdgaming 2010-05-22
  • 打赏
  • 举报
回复
list=(ArrayList)ol.getApplication().getAttribute("check_uri");
list.add(map);//这一句出错 不管是 map 形式 还是 一个自定义的字符串都会报错 java.lang.NullPointerException

list是空啊

你这句有问题啊(ArrayList)ol.getApplication().getAttribute("check_uri");
你能这么转换为list 吗????你确认
qindwt1 2010-05-22
  • 打赏
  • 举报
回复
问题解决了,就是list=(ArrayList)ol.getApplication().getAttribute("check_uri"); 是空值。
new OnLineListener(); 其包含static 变量 application。不会永远为空。但第一次肯定是空值。要判断下就好了。
恶魔 2010-05-22
  • 打赏
  • 举报
回复
学习一下吧
crazylaa 2010-05-22
  • 打赏
  • 举报
回复
OnLineListener ol=new OnLineListener(); (1)
HashMap map=new HashMap();
map.put("username", username);
map.put("random", random);
ArrayList list=new ArrayList();
list=(ArrayList)ol.getApplication().getAttribute("check_uri"); (2)
list.add(map);//这一句出错 不管是 map 形式 还是 一个自定义的字符串都会报错 java.lang.NullPointerException
ol.getApplication().setAttribute("check_uri",list);

-----------》》》》》》》
(1)每次execute,这里是个new的ol,除非你在构造方法或者getApplication().或者啥里面对check_uri进行了初始化,
(2)这里得到的永远是个null。下面的add肯定出错。
jumpheightway 2010-05-22
  • 打赏
  • 举报
回复
list=(ArrayList)ol.getApplication().getAttribute("check_uri");
错误是这句
不是你的下面那句

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧