0

我收到一个 ArrayList 的奇怪错误,下面是代码(请注意,此代码是从在线 servlet 示例中复制的,我是 JAVA 新手)。

JSP:

<%@page import="p.SecondExample"%>
<%@ page language="java" import="java.util.*;"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE>Servlet Application</TITLE>

  <script language="javascript">
 function editRecord(id){

 window.location.href="editServlet/"+id; 
 }


function deleteRecord(id){

window.location.href="deleteUser/"+id; 
}
</script>
</HEAD>

<BODY>
 <br>
  <table align="center">

 </table>
 <br>
 <table width="600px"  align="center" style="background-color:#EDF6EA;border:1px solid #000000;">
<tr><td colspan=9 align="center" height="10px"></td></tr>

<tr><td colspan=9 align="center"><!-- <a href="addUser.jsp" style="font-weight:bold;color:#cc0000;">Add New User</a>--></td></tr>
 <tr><td colspan=9 align="center" height="10px"></td></tr>
    <tr style="background-color:#7BA88B;font-weight:bold;">
          <td>Sector Segment</td><td>Color</td>

</tr>
<%
String bgcolor="";
int count=0;
List viewList = new ArrayList();
Iterator  viewItr;

SecondExample se = new SecondExample();
se.doPost(request, response);

if(request.getAttribute("userList")!=null && request.getAttribute("userList")!="")
{
    List userList =  (ArrayList)request.getAttribute("userList");
    Iterator itr = userList.iterator();
    System.out.println(userList);
    while(itr.hasNext())
    {

        if(count%2==0)
        {
         bgcolor = "#C8E2D1";
        }
        else
        {

            bgcolor = "#EAF8EF";
        }

        viewList = (ArrayList)itr.next();
        int id = Integer.parseInt(viewList.get(0).toString());
        viewItr = viewList.iterator();
        %>
        <tr style="background-color:<%=bgcolor%>;">
        <%  
        while(viewItr.hasNext())
        {

            %>
            <td><%=viewItr.next()%></td>

            <%

        }
        count++;
        %>
        <td><input type="button" name="edit" value="Edit" style="background-color:#49743D;font-weight:bold;color:#ffffff;" onclick="editRecord(<%=id%>);" ></td>
            <td><input type="button" name="delete" style="background-color:#ff0000;font-weight:bold;;color:#ffffff;" value="Delete" onclick="deleteRecord(<%=id%>);"></td>
        </tr>
        <%
    }
}
if(count==0)
{
    %>
    <tr><td colspan="9" align="center">&nbsp;</td></tr>
        <tr><td colspan="9" align="center">No Record Avaliable</td></tr>
    <%
}
%>
   <tr><td colspan=9 align="center" height="2px"></td></tr>
   </table> 
   </BODY>
   </HTML>

在调试中,错误似乎发生在:

        viewList = (ArrayList)itr.next();
        int id = Integer.parseInt(viewList.get(0).toString());
        viewItr = viewList.iterator();

我的错误显示为:

org.apache.jasper.JasperException: java.lang.ClassCastException: java.lang.String    cannot be cast to java.util.ArrayList

我不太确定为什么或如何制作下一个字符串。任何帮助是极大的赞赏。

4

1 回答 1

3

是的,itr.next()返回 a String,它不能被转换成ArrayList

String value = (String) itr.next();
int id = Integer.parseInt(value);

viewArray现在也可以删除您在其中迭代的以下几行代码。

于 2013-03-08T19:25:01.047 回答