aspose file tools*
The moose likes Struts and the fly likes Cannot create iterator for this collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Cannot create iterator for this collection" Watch "Cannot create iterator for this collection" New topic

Cannot create iterator for this collection

Tokunbo Oke
Ranch Hand

Joined: Sep 09, 2003
Posts: 57
Help please!

Process flow
Index.jsp is supposed to execute the getCustomer() method in customerAction class. The customerAction is supposed to put a customers ArrayList object in the request scope. the customer.jsp screen is supposed to iterate through the customer ArrayList object that was placed in the request scope and display the data in their respective fields.

My objective is to display the data brought back from the database.

When I debugged CustomerAction class with these lines of code:

Iterator i = customers.iterator();
while( i.hasNext() ){
System.out.println( "((CustomerBean) " + ((CustomerBean) );

, I got the following out from the Arraylist object that is placed the the request scope:

((CustomerBean) OLA
((CustomerBean) OLATEST
((CustomerBean) SAUD

Please tell me the cause of the error and how to work round it. In any case, is there a more efficient way of achieving my objective?


Ola Oke
When I execute/run index.jsp screen I get the error below:

org.apache.jasper.JasperException: Cannot create iterator for this collection
at org.apache.jasper.servlet.JspServletWrapper.service(
at org.apache.jasper.servlet.JspServlet.serviceJspFile(
at org.apache.jasper.servlet.JspServlet.service(
at javax.servlet.http.HttpServlet.service(
at org.apache.catalina.core.ApplicationDispatcher.invoke(
at org.apache.catalina.core.ApplicationDispatcher.doForward(
at org.apache.catalina.core.ApplicationDispatcher.forward(
at org.apache.struts.action.RequestProcessor.doForward(
at org.apache.struts.action.RequestProcessor.processForwardConfig(


index.jsp (This is the JSP the processing that leads to the error):
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<jsp:forward page="/"/>

<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<logic:redirect forward="/"/>

Redirect default requests to Welcome global ActionForward.
By using a redirect, the user-agent will change address to match the path of our Welcome ActionForward.


<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>

<title><bean:message key="fieldL.title" /></title>

<body bgcolor="#999966" text="#000000" link="#0033FF" vlink="#FFFF00" alink="#00FF00">
<table width="500"
border="0" cellspacing="0" cellpadding="0">
<td> </td>
<td height="68" width="48%">
<div align="left">
<img src="file:///D|/Apps/Apache/Tomcat-4.1.27/webapps/employees/images/wxmainlogowhitespace.gif">
<td> </td>
<html:form action="/customer"
type="com.cargo.gen.CustomerForm" >
<table width="500" border="0">
<td width="54"><bean:message key="fieldL.customerNo"/>:</td>
<td width="37"><html:text property="customerNo" maxlength="5" size="5"/></td>
<td width="54"><bean:message key="fieldL.firstName" />:</td>
<td width="37"><html:text property="firstName" /></td>
<td width="54"><bean:message key="fieldL.lastName" />:</td>
<td width="37"><html:text property="lastName" /></td>
<td><bean:message key="fieldL.dob"/>:</td>
<td><html:text property="dob" /></td>
<td><bean:message key="" />:</td>
<td><html:text property="sex" /></td>
<td><bean:message key="fieldL.address"/>:</td>
<td><html:text property="address" size="30"/></td>
<td><bean:message key="fieldL.addressCode"/>:</td>
<td><html:text property="addressCode" size="10"/></td>
<td height="37"><bean:message key="" />:</td>
<td><html:text property="email" /></td>
<td><bean:message key="" />:</td>
<td><html:text property="phone" /></td>
<td><bean:message key="fieldL.regDate" />:</td>
<td><html:text property="regDate" /></td>
<td width="54"><bean:message key="fieldL.status"/>:</td>
<td width="37"><html:text property="status" maxlength="6" size=""/></td>
<td width="64"><bean:message key="fieldL.creditRating"/>:</td>
<td width="30"><html:text property="creditRating" maxlength="6" size="1"/></td>
<logic:iterate id="customer" name="customers">
<tr align="left">
<td><bean:write name="customer" property="customerNo"/>:</td>
<td><bean:write name="customer" property="firstName"/>:</td>

<html:hidden property="methodToExecute" value="error"/>
<SCRIPT>function set(target) {document.forms[0].methodToExecute.value=target;}</SCRIPT>
<html:submit on click="set('regCustomer')">Register Customer</html:submit>
<html:submit on click="set('getCustomer')">Get Customer</html:submit>
<html:submit on click="set('updateCustomer');">Save Changes</html:submit>



struts-config.xml file:
<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"


<!-- ==================================== Data Source Configuration -->

<data-source type="org.apache.commons.dbcp.BasicDataSource">
<set-property property="driverClassName" value="com.mysql.jdbc.Driver" />
<set-property property="url" value="jdbc:mysql://localhost/cargo" />
<set-property property="username" value="ola" />
<set-property property="password" value="ola" />

<!-- ======================================== Form Bean Definitions -->

<form-bean name="customerForm" type="com.cargo.gen.CustomerForm"/>

<!-- sample form bean descriptor for a DynaActionForm









end sample -->


<forward name="cust" path="/customer"/>

<!-- Default "Welcome" action -->
<!-- Forwards to Welcome.jsp -->
<action path="/customer"
<forward name="success" path="/jsp/customer.jsp"/>
<forward name="failure" path="/jsp/failure.jsp"/>

<message-resources parameter="com.cargo.resources.application"/>


Action Class:
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import java.util.ArrayList;
import java.util.Iterator;

public class CustomerAction extends DispatchAction {

public ActionForward getCustomer(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {

DbInterface dbInterface = new DbInterface();
ArrayList customers = dbInterface.getCustomer( getDataSource(request) );
request.setAttribute("customers", "customers");

Iterator i = customers.iterator();
while( i.hasNext() ){
System.out.println( "((CustomerBean) " + ((CustomerBean) );

return mapping.findForward("success");

public ActionForward updateCustomer(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
return mapping.findForward("success");

public ActionForward regCustomer(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
System.out.println("Register customer");
return mapping.findForward("success");

sarah Marsh
Ranch Hand

Joined: Mar 06, 2001
Posts: 282
In your Form Bean Definitions, you only defined username and password.

But in the JSP, you use customerNo and firstName:
<logic:iterate id="customer" name="customers">
<tr align="left">
<td><bean:write name="customer" property="customerNo"/>:</td>
<td><bean:write name="customer" property="firstName"/>:</td>

Make them consistance to each other and try if it can slove your problem.
Tokunbo Oke
Ranch Hand

Joined: Sep 09, 2003
Posts: 57
The formbean that I believe you are referring to is just an example embeded inside a comment. I have edited and reposted.

Thanks anyway.
I agree. Here's the link:
subject: Cannot create iterator for this collection
Similar Threads
How to populate fields in a form with data retrieved from a database table
change 'action' path dynamically inside JSP.
Cannot create iterator for this...ArrayList()...
Values not poulating in the dropdown box.
Displaying a table with in a table with DWR data