aspose file tools*
The moose likes Struts and the fly likes problem adding dynamically a new element to an ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "problem adding dynamically a new element to an ArrayList " Watch "problem adding dynamically a new element to an ArrayList " New topic
Author

problem adding dynamically a new element to an ArrayList

Kristof Camelbeke
Ranch Hand

Joined: Nov 28, 2001
Posts: 97
Hello I don't know if this is the correct forum since it's a jsp-struts-javascript issue...

Anyway here's the situation.

A page is loaded with a form which contains a List....

This list is iterated as follows:




The properties of this list are stored and passed via <html:hidden> properties to the javascript function where the html code is generated.
Here's an example of the generated html output for 1 property:


All works well when I submit the form without adding a new element

When the the user decides to create a new element the same javascript function is called to create the html code indexing the proper form elements.
When the submit is done I get an ArrayOutOfBoundsException at org.apache.commons.beanutils.PropertyUtilsBean.getIndexedProperty(PropertyUtilsBean.java:433)

So how do you increment the list size? I use the getXXX(index) in my form but it never gets there when the index is larger thant the list size.
I've seen many examples but most are for editing the current list not adding new elements...

Any input is mostly appreciated!

Thank you
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61205
    
  66

As this appears to be an issue populating a Struts form, I've moved it along to the Struts forum.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Lorand Komaromi
Ranch Hand

Joined: Oct 08, 2009
Posts: 276
Are you using Struts 1..?


OCJP 6 (93%)
Kristof Camelbeke
Ranch Hand

Joined: Nov 28, 2001
Posts: 97
We're still using 1.2.7...
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61205
    
  66

Is your bean set up properly to accept indexed properties? The rules are fairly stringent.

Here's something I found That might give you a wedge to start exploring...
Kristof Camelbeke
Ranch Hand

Joined: Nov 28, 2001
Posts: 97
Thanks Bear for the link to the article.

I have a getXX(index) method which is called for the existing items but I have no setter and that might be the problem for the ArrayIndexOutOfBoundsException as mentionned in the article:

The indexed getter and setter methods may throw a java.lang.ArrayIndexOutOfBoundsException runtime exception if an index is used that is outside the current array bounds. In order to change the size of the array you must use the array setter method to set a new (or updated) array.


I'll try this first out thing monday morning...
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Kristof Camelbeke wrote:
I'll try this first out thing monday morning...


why monday?.
Kristof Camelbeke
Ranch Hand

Joined: Nov 28, 2001
Posts: 97
It's on my pc at work and since they don't give us laptops I don't have any other option than to wait till monday morning
Kristof Camelbeke
Ranch Hand

Joined: Nov 28, 2001
Posts: 97
I've added the indexed setters/getters on my bean but they never get called... I even added them on the ActionForm but also no result....

here's the generated html output:



To resume my 'aDto' has a List 'aList' wich references Object items where serial number is a String property.
I've added indexed getter/setters in this dto for the list and for the properties of the Object items but they never get called.

When debugging I see that the public Object getIndexedProperty(Object bean, String name) method is called from the PropertyUtilsBean and that the line return (getIndexedProperty(bean, name, index)); contains the bean with index 2 and the index 3 which causes the ArrayOutOfBoundsExcption....


Kristof Camelbeke
Ranch Hand

Joined: Nov 28, 2001
Posts: 97
Hello,

Anybody has an idea what the cause of this problem is?

Thanks...
Tom Rispoli
Ranch Hand

Joined: Aug 29, 2008
Posts: 349
try posting the relevant declarations, getters, and setters for your beans. The problem is probably there somewhere.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem adding dynamically a new element to an ArrayList