File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes Cannot create iterator for FormBean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Cannot create iterator for FormBean" Watch "Cannot create iterator for FormBean" New topic
Author

Cannot create iterator for FormBean

Shailesh Pillai
Ranch Hand

Joined: Jan 24, 2006
Posts: 78
Hello Ranchers,
I am getting this exception:
Cannot create iterator for EmpForm@cle333
I tried out some solutions given earlier in this forum. But in vain...

My jsp:
-----------------------------------
<html:select property="empNo">
<html ptions collection="EmpForm" property="empNo" labelProperty="empLbl"/>
</html:select>
-----------------------------------

In my Action class i am passing an ArrayList as the parameter to the setter method in EmpForm.
In EmpForm i have two objects : empNo and empLbl (both are ArrayList objects)

Can someone sort it out where i am going wrong.
[ May 01, 2006: Message edited by: Shailesh Pillai ]
Jass Singh
Ranch Hand

Joined: Mar 30, 2006
Posts: 52
Originally posted by Shailesh Pillai:
My jsp:
-----------------------------------
<html:select property="empNo">
<html options collection="EmpForm" property="empNo" labelProperty="empLbl"/>
</html:select>
-----------------------------------

In my Action class i am passing an ArrayList as the parameter to the setter method in EmpForm.
In EmpForm i have two objects : empNo and empLbl (both are ArrayList objects)

Can someone sort it out where i am going wrong.

[ May 01, 2006: Message edited by: Shailesh Pillai ]


Shailesh, you are getting it wrong. You don't need two ArrayList attributes in EmpForm.

First make a value Object(VO) class say Employee having two attributes empNo and empLbl.



Now have only one ArrayList in EmpForm say Employees. This should be a collection of Employee objects, each containing empNo & empLbl for one employee.

In JSP change options tag to following:

<html options collection="Employees" property="empNo" labelProperty="empLbl"/>

That Employee collection is part of EmpForm is implicit here. If you want to make it explicit then you may add name="EmpForm" attribute to above tag.
The above tag says from EmpForm fecth a collection named Employees. And on each element of Employees invoke getEmpNo() and getEmpLbl() methods.

Regards,
Jass
Shailesh Pillai
Ranch Hand

Joined: Jan 24, 2006
Posts: 78
Thanks a lot for ur reply,
But tell me one thing in my Action class 'EmpAction' i am getting the values from the database & storing it in an array. In the 'EmpForm' how will i get the array and populate for each elements of the ArrayList Employees in 'EmpForm'.

Following is my EmpForm

Pls suggest the changes required in EmpForm.
[ May 03, 2006: Message edited by: Shailesh Pillai ]
Jass Singh
Ranch Hand

Joined: Mar 30, 2006
Posts: 52
Originally posted by Shailesh Pillai:
[QB]Thanks a lot for ur reply,
But tell me one thing in my Action class 'EmpAction' i am getting the values from the database & storing it in an array. In the 'EmpForm' how will i get the array and populate for each elements of the ArrayList Employees in 'EmpForm'.

Should i make use of 'org.apache.struts.util.LabelValueBean'.
And if i use LabelValueBean i don't think i require the 'Employee' class.

Following is my EmpForm

[QB]



Its simple, your setEmployees () and getEmployees() should be like following:




Move all the code written in setEmployees (String[] results) to your action class and from there call setEmployees(ArrayList Employees) to set ArrayList in it .

Yes, you can also use 'org.apache.struts.util.LabelValueBean', but since I have never used it, I can't comment how to use it.


Regards,
Jass
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
First off...for something like the code you posted to work, you would have to move the line "Employee emp=new Employee();" inside the for loop. Otherwise, each loop just updates the same instance and you will end up with a list full of duplicate objects.

It is hard to say exactly what your code needs to look like without knowing what data types you are getting back from the database. I use a class similar to LabelValueBean, with the properties id and label. I often get back a Collection of data objects from the business tier, and populate a List of ListValue objects on the Form with a method like this:


- Brent
Jass Singh
Ranch Hand

Joined: Mar 30, 2006
Posts: 52
Originally posted by Brent Sterling:
First off...for something like the code you posted to work, you would have to move the line "Employee emp=new Employee();" inside the for loop. Otherwise, each loop just updates the same instance and you will end up with a list full of duplicate objects.

- Brent


Ahh... how could I miss that.


Regards,
Jass
Shailesh Pillai
Ranch Hand

Joined: Jan 24, 2006
Posts: 78
Thanks for your responses,
Jass i tried out your solution but getting a null pointer exception. May be,the arraylist is throwing such an exception.

I am stuck because my funda regarding "how to populate an arraylist and get those values in the html:select box" is NOT clear.

What i am trying to do is...
I have a link in a page. On clicking it i get a Form having a dropdown box that contains - the EmpCode of all Employees (which is of type int) from the database and populate it in html:select

My Action classes are EmpAction & Employee (as specified by Jass Singh).
My Formbean is EmpForm.

I am able to get values from database in an array.
But stuck on ....... how to pass it to EmpForm.

EmpForm :


EmpAction :


Employee class is as specified by Jass Singh.
Pls help out as i am stuck on this for quite some days now.
[ May 04, 2006: Message edited by: Shailesh Pillai ]
Jass Singh
Ranch Hand

Joined: Mar 30, 2006
Posts: 52
EmpAction :


Employee class is as specified by Jass Singh.
Pls help out as i am stuck on this for quite some days now.

[ May 04, 2006: Message edited by: Shailesh Pillai ][/qb]<hr></blockquote>

1. Shailesh, your EmpForm seems alright. However in EmpAction you seem to miss the point raised in 1st paragraph of Brent's post i.e. move line Employee emp=new Employee(); inside the loop.

2. EmpForm empForm=new EmpForm(); This line is the center of problem. In struts Action you never create Form bean instance. Get EmpForm bean instance by type casting ActionForm that you get as parameter in execute() method of EmpAction.

3.Another issue is you are using two loops. One to get data form rs to results array and another to creat ArryaList from results. You can manage with one loop only.

here is the code that should go to execute() of EmpAction:



I assume you are doing other handling of struts like forwarding etc correctly.

Regards,
Jass
[ May 04, 2006: Message edited by: Jass Singh ]
Shailesh Pillai
Ranch Hand

Joined: Jan 24, 2006
Posts: 78
Thanks Jass very much.
The main problem was with empList.add(emp);
Instead of passing the object i was passing String[] results.

But, now i am getting an empty drop-down box with no values.
I am trying out....
[ May 04, 2006: Message edited by: Shailesh Pillai ]
Raghavendra nandavar
Ranch Hand

Joined: Jun 26, 2005
Posts: 231
Originally posted by Shailesh Pillai:
Can any one figure out.....


As jass said, create an Employee value object with two feilds, i.e id and the label. Then, whatever data you are getting from the database, iterate the collection and add the list of value objects into an ArrayList object.

Then you need to iterate the collection in the jsp as follows :




Here empList is the collection of value objects, where in you are iterating and displaying the label and value of the option is nothing but the emp num.
Shailesh Pillai
Ranch Hand

Joined: Jan 24, 2006
Posts: 78
Hello Raghavendra,



Can you tell me what this iterate id is for?

And, when i am using the above code in my jsp.
I am getting the exception :
-----------
No getter method for property: "Employees" of bean: "EmpForm"
------------
[ May 04, 2006: Message edited by: Shailesh Pillai ]
Raghavendra nandavar
Ranch Hand

Joined: Jun 26, 2005
Posts: 231
Originally posted by Shailesh Pillai:
Hello Raghavendra,



Can you tell me what this iterate id is for?

And, when i am using the above code in my jsp.
I am getting the exception :
-----------
No getter method for property: "Employees" of bean: "EmpForm"
------------

[ May 04, 2006: Message edited by: Shailesh Pillai ]



In struts, logic : iterate tag expects an id, and here element is an id
for the tag, which is in turn used to iterate your collection (ArrayList)
and display the values.


No getter method for property "Employees" - I think you are storing your
collection of value objects in an arrayList, you have to create a form bean object for that by name "Employees" or any other object, and display the same in the jsp. Hope this helps.
Jass Singh
Ranch Hand

Joined: Mar 30, 2006
Posts: 52
Originally posted by Shailesh Pillai:

But, now i am getting an empty drop-down box with no values.
I am trying out....



1.Are you sure that your query is returning any data.

2. Make sure that code in execute() of EmpAction is just like what i wrote in post#8 above.

3. To see if ArrayList is being populated, add follwing line after while{...} loop:
System.out.println(empList.size());
This will tell you how many Employee objects are added to ArrayList

If you still get problem, post code of your execute() again.

Regards,
Jass
Shailesh Pillai
Ranch Hand

Joined: Jan 24, 2006
Posts: 78
Hello Jass,
I tested the results returned from the database as follows:
if (empList.isEmpty())
return mapping.findForward("failure");
else
return mapping.findForward("success");

I am forwarded to success.
Yes, i am getting values from database.
-----------------------------
Do i need to do logic:iterate in my JSP as specified by Raghavendra.
-----------------------------
For your reference :
EmpAction:

[ May 05, 2006: Message edited by: Shailesh Pillai ]
Jass Singh
Ranch Hand

Joined: Mar 30, 2006
Posts: 52
Originally posted by Shailesh Pillai:
Do i need to do logic:iterate in my JSP as specified by Raghavendra.


Note: Allover in code below I used "empForm" assuming that in struts-config.xml you have defined it like <form-bean name="empForm" ..../>. If you have used some other name then replace "empForm" with that name.

Yes you can use that as well. Use:



Another options is to use follwoing. This is better alternative to <html options... :



But main concern is to check if employees arraylist that you are getting in JSP has some values or is it getting reset somewhere. To check length of arraylist in JSP do following:



Regards,
Jass
Shailesh Pillai
Ranch Hand

Joined: Jan 24, 2006
Posts: 78
Hello Jass,
Did check with the following condition

if (empForm.getEmployees().size() > 0)
return mapping.findForward("success");
else
return mapping.findForward("failure");

I am forwarded to success. I am still getting empty select box.
[ May 05, 2006: Message edited by: Shailesh Pillai ]
Jass Singh
Ranch Hand

Joined: Mar 30, 2006
Posts: 52

[/qb]<hr></blockquote>
You missed the point in my above post. I was asking for cheking arraylist size in JSP. Try doing that.

Regarding following check done by you:



Are you sure "success" & "failure" are not pointing to same JSP.
Check <forward.. > tag in struts-config.xml for "failure" and "success" and ensure that.

Regards,
Jass
[ May 05, 2006: Message edited by: Jass Singh ]
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
Wow...this has turned out to be quite a long thread. I will assure you Shailesh that once you get the hang of thing it will not always be this difficult.

The Java code you posted looks good. You are populating the form that was passed in, so the list should be populated on the request. Maybe if you posted your current JSP somebody would see the issue.

- Brent
Shailesh Pillai
Ranch Hand

Joined: Jan 24, 2006
Posts: 78
Thanks a LOT Jass and Brent,
It worked.
Nothing was wrong with the Java Code.
The problem was - I was missing the semi-colon between
<html optionsCollection>.
I think you might have also missed it in the post - Jass.
But once again thanks a lot, the funda is clear now.

Regards,
Shailesh
[ May 09, 2006: Message edited by: Shailesh Pillai ]
lubna kausar
Ranch Hand

Joined: May 06, 2006
Posts: 51
hey friendz... if the code given is working then i also require it...
but i want this thing to be done on pageload not on any click...
so can any one guide me how to get this at pageload time???
hoping to get a solution soon
thanx in advance...
Jass Singh
Ranch Hand

Joined: Mar 30, 2006
Posts: 52
Originally posted by Shailesh Pillai:
Thanks a LOT Jass and Brent,
It worked.
Nothing was wrong with the Java Code.
The problem was - I was missing the semi-colon between
<html optionsCollection>.
I think you might have also missed it in the post - Jass.
But once again thanks a lot, the funda is clear now.

Regards,
Shailesh

[ May 09, 2006: Message edited by: Shailesh Pillai ]


No I didn't miss it in my post. The real culprit is smiley code of Javaranch. Combination of : and small 'o' is interpreted as a smiley. See your 1st post, it showing a smiley in red color.

Even in my post though I put colon : between <html and options but it due some procssig for smilies it got removed. But anyways, you found your way thats the main point.

Regards,
Jass
Yogesh Gandhi
Ranch Hand

Joined: Dec 05, 2008
Posts: 212
Hi Friends,

Can someone send me the working code

that can populate the list box from a bean.

Its really urgent. i am trying it since 2 days, but till not unsuccessful.

my e-mail

yogesh_gandhi@infosys.com
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Please post a new thread.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Cannot create iterator for FormBean