File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dynamic text fields gives java.lang.IndexOutOfBoundsException

 
Sebastian Grännby
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
*bump*
 
Nishan Patel
Ranch Hand
Posts: 689
Eclipse IDE Java Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


 
Sebastian Grännby
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 689
Eclipse IDE Java Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I solved my problem by using this solution for the form instead. Also the jsp was rewritten:

form:
 
Deeps Mistry
Ranch Hand
Posts: 189
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 189
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic