我正在开发一个评论论坛,人们会发表评论,并且可以在发布后看到他们的评论,但我在 servlet 中遇到了空指针异常
。
index.jsp
<form action="Comment" method="post">
<textarea style="resize: none;" cols="30" rows="3" id="myTextarea" name="myTextarea"></textarea>
</form>
评论servlet
try{
String comment=request.getParameter("myTextarea");
ArrayList al1=null;
ArrayList emp_list =new ArrayList();
al1.add(comment);
emp_list.add(al1);
request.setAttribute("empList",emp_list);
String nextJSP = "/result.jsp";
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);
dispatcher.forward(request,response);
}
catch(Exception e){
out.println("exception"+e);//exception coming
}
最佳答案
当您尝试添加时,al1
将为空:
ArrayList al1=null;
ArrayList emp_list =new ArrayList();
al1.add(comment);
尝试将第一行更改为:
ArrayList al1 = new ArrayList();
这将导致将数组列表内的数组列表转发到下一个 jsp 页面。这样做可能会更简单:
ArrayList emp_list =new ArrayList();
emp_list.add(comment);
...并完全删除变量 al1
。
关于java - java servlet中出现空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10204022/