我想弄清楚爲什麼我得到一個空指針異常。如何從一個servlet發送arraylist到其他servlet
當我嘗試執行addformservlet。 在這個問題上幫助我我是新來的jsp servlets。
請幫助我..
,我都堅持了一部分......
addform的servlet:
processRequest(HttpRequest req,HttpResponse res)
{
String []erlist=null;
PrintWriter out = response.getWriter();
response.setContentType("text/html;charset=UTF-8");
try {
out.println("<html>");
out.println("<head>");
out.println("<title>Add dvd :</title>");
out.println("</head>");
out.println("<body bgcolor='#E6599'>");
if(erlist!=null)
{
out.println("<p>");
out.println("<font color='red'>Correct the following errors:");
out.println("<ul>");
for(int i=0;i<erlist.length;i++)
out.print("<li>"+erlist[i]+"</li>");
out.println("</ul>");
out.println("</font>");
out.println("<a href='index.jsp'> back to index page</a>");
out.println("</p>");
}
erlist=(String [])request.getSession().getAttribute("elist");// i get errlist from dvdaction.java
out.println("<h2>Add DVD: </h2>");
out.println("<form action='dvdaction' method='post' >");//post page to dvdaction.java
//some codes here....
out.println("</form>");
out.println("</body>");
out.println("</html>");
}
//dvdaction.java:
processRequest(HttpRequest req,HttpResponse res)
{
String err[]=null;
try {
title=(String)request.getParameter("title");
int i=0;
if(title.trim().length()==0)
{
err[i]="Please add the title field";
i++;
}
if(err.length==0)
{
//some codes here...
}
request.getSession().setAttribute("elist", err);// i set err Arraylist here...
RequestDispatcher rd=request.getRequestDispatcher("adddvdformservlet");
rd.forward(request, response);
}
我看不到你在任何地方創建'err'並且Java數組不是動態的。基本上,你應該得到'NullPointerException's。 – 2014-11-08 18:18:02
i crested err []在dvdaction.java,第二個processrequest() – 2014-11-08 18:21:21
設置之前'err [i] =「請添加標題欄';'你應該初始化你想要的錯誤或項目的數量 – 2014-11-09 23:28:23