aspose file tools*
The moose likes Struts and the fly likes Dynamic text fields gives java.lang.IndexOutOfBoundsException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Dynamic text fields gives java.lang.IndexOutOfBoundsException " Watch "Dynamic text fields gives java.lang.IndexOutOfBoundsException " New topic
Author

Dynamic text fields gives java.lang.IndexOutOfBoundsException

Sebastian Grännby
Greenhorn

Joined: Jan 05, 2009
Posts: 6
Hi

Im trying to create dynamic textfields that are added when pressing a (+) button however all my attempts end in e.g. java.lang.IndexOutOfBoundsException: Index: 3, Size: 1 when beanUtils.populate is being performed on submit. The size is always 1 so it's never growing. Im having a Javabean object named ContactNameBean containing 5 Strings which are represented by one textfield each. When I press the (+) button I want to create a new instance of this JavaBean object (5 new textfields should be displayed). Note that I always have 1 javabean object added in the action before the page is being displayed. So there is always 1 ContactNameBean created. Then I have the (+) button to add more objects.
The index variable is what should keep track of the size of the and it seems to get the correct values however no objects are being added to the collection. I do not think the index variable has wrong values. It's just the objects never seem to have been created when submitted.

Note: If I type this code in my reset() method I can see that there actually is a saved value in the field however it is not in the form:


Here is my code:

ActionForm
private List<ContactNameBean> contactNames = new ArrayList<ContactNameBean>();


Javascript


Any help is greatly appreciated
Sebastian Grännby
Greenhorn

Joined: Jan 05, 2009
Posts: 6
*bump*
Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 688

Hi,

First of all your index starts with 1 integer value and your List contactNames starts with 0 integer value.

So, when contactNames [index] reached at last element it is 1 more than your list size.

So, initialize your index to 0 and then check.



Thanks, Nishan Patel
SCJP 1.5, SCWCD 1.5, OCPJWSD Java Developer,My Blog
Sebastian Grännby
Greenhorn

Joined: Jan 05, 2009
Posts: 6
Hi

I use index = 1 because I always have one object in the list when the page is initialized. Hence the next contactNames[index] should get 1. Setting it to 0 gives same problem. Whatever I do size is always 1 why is that? If I press the (+) button four times I still get that index is 4 and size is 1
Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 688


Hi,

Just check your list.size and what it returns ?

If you talking about 4 is your index and your list sizt is 1 and if you use list[index] then it but obvious give you array index out of bound.

so make your look like

for(int i=0;i<list.size;i++){


}

remove your index.


>
Sebastian Grännby
Greenhorn

Joined: Jan 05, 2009
Posts: 6
Nishan Patel wrote:
Hi,

Just check your list.size and what it returns ?

If you talking about 4 is your index and your list sizt is 1 and if you use list[index] then it but obvious give you array index out of bound.

so make your look like

for(int i=0;i<list.size;i++){


}

remove your index.
>


I don't get it. Where and how should I check my size and how would it help?
If I press the (+) button 3 times and I already have a ContactNameBean object assigned I will get the following fields (index is correct when checking with alert(index) at the top of the method):
contactNames[0].contactName (Note that this one is already in the list and is not added with a (+) being pressed)(and contactPhone, contactEmail and so on)
contactNames[1].contactName (and contactPhone, contactEmail and so on)
contactNames[2].contactName (and contactPhone, contactEmail and so on)
contactNames[3].contactName (and contactPhone, contactEmail and so on)

However when I press submit only 1 ContactNameBean object is inside my list (as it says size is 1 in the error message but the index is correct in the way that it matches what it should be in the list). So it seems that the list isn't dynamically growing as the objects don't seem to be added.
Sebastian Grännby
Greenhorn

Joined: Jan 05, 2009
Posts: 6
Hi I solved my problem by using this solution for the form instead. Also the jsp was rewritten:

form:
Deeps Mistry
Ranch Hand

Joined: Jan 31, 2009
Posts: 189
Sebastian Grännby wrote:Hi I solved my problem by using this solution for the form instead. Also the jsp was rewritten:

form:


Hey Sebastian,

My problem is similar to yours. I want to add a row of text boxes on click of a button using struts <html:text> tag.

Please help me out. Could you please post your code :Action form, action class and jsp?

Thanks
Sebastian Grännby
Greenhorn

Joined: Jan 05, 2009
Posts: 6
Deeps Mistry wrote:
Sebastian Grännby wrote:Hi I solved my problem by using this solution for the form instead. Also the jsp was rewritten:

form:


Hey Sebastian,

My problem is similar to yours. I want to add a row of text boxes on click of a button using struts <html:text> tag.

Please help me out. Could you please post your code :Action form, action class and jsp?

Thanks



Hi

Sorry but I no longer have access to the code as I left that particular project at work...
Deeps Mistry
Ranch Hand

Joined: Jan 31, 2009
Posts: 189
Sebastian Grännby wrote:
Deeps Mistry wrote:
Sebastian Grännby wrote:Hi I solved my problem by using this solution for the form instead. Also the jsp was rewritten:

form:


Hey Sebastian,

My problem is similar to yours. I want to add a row of text boxes on click of a button using struts <html:text> tag.

Please help me out. Could you please post your code :Action form, action class and jsp?

Thanks



Hi

Sorry but I no longer have access to the code as I left that particular project at work...


Hey..no problem .Thanks for your prompt reply.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dynamic text fields gives java.lang.IndexOutOfBoundsException