Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dynamic Form Elements - Exception

 
santhi deeksha
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a form with a table which has a single row of three columns initially. I have a link which adds an extra row everytime it is clicked. Since I do not know how many might be added I used an arrayList. Here's how my JSP looks.


And this is how I am adding a row using Jquery


In my form, I wrote getter and setter for a member (of each iteration) apart from the getter and setter for the arrayList.



Now, the above code works fine if I try to add a single row and submit. But if I try to add more than one row at a time and submit, I get an IndexOutOfBoundsException. Can you please tell me what I am doing wrong and how can I fix it?
 
Shankar Tanikella
Ranch Hand
Posts: 329
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you put the log trace here
 
santhi deeksha
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Shankar, here it is. Like I mentioned earlier, I have one initial row. I added 3 more one by one. So I have 4 rows when I tried to add 2 more at once.

 
Shankar Tanikella
Ranch Hand
Posts: 329
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
after adding all 4 rows could you see the "view source" of the JSP and see how the elements of the document are considered.
 
santhi deeksha
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shankar, Yes and this is how it looks.

 
Tom Rispoli
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This line in your getter looks strange to me:



What is "List" and what are you trying to do with it here? Did you mean to use programmeTeamList?
 
santhi deeksha
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tom, yes it is in fact programmeTeamList. I might have accidentally deleted it while trying to copy it over here. Sorry about that.

 
Shankar Tanikella
Ranch Hand
Posts: 329
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as you say that the problem is while submitting the form, i am more interested in the setter in the form

And what is on line 102 in your form anyways?

Am I wasting your time, actually this is how i debug a problem step by step, i first check the log and the corresponding line number. if that doesn't work out then i go for view source to get the exact picture what is being considered and then if that even doesn't help i go for syso , so be it

Could you just put system out in the form for index as below

and SOP in setter as well, are you overloading the getter and setter by any chance, which version of struts are you using, just to try in my local.

 
santhi deeksha
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Shankar, I appreciate you trying to help me out. It is not a waste of my time if I am learning something :-). Thank you!

I am using Struts 1.1. Line 102 in my code is the return statement in the getter. I included the SOPs as you suggested and I did get the one in getter printed as "processing current index: 7" and my Exception reads the same index "java.lang.IndexOutOfBoundsException: Index: 7, Size: 7"
 
santhi deeksha
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh and I forgot. I verified and no I am not overloading the getter or setter.
 
Shankar Tanikella
Ranch Hand
Posts: 329
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to know i am looking into the links here and here to see where are we going wrong. It is really worth reading it once. Till now i couldn't see any problem

Interesting to know that you are getting 7 from somewhere, if i see your previous post it has index as 5 in the log? did you add anything extra?

if index is the cause, it looks like this line is the problem
 
Tom Rispoli
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the problem is that your getter method is only set up to handle the scenario where the index passed into the method will only increase your array's size by one. The problem is that the index coming in could require you to add more than one element to your list in order to make the index coming in valid. I think you need to add a loop to your getter to keep adding elements to your list until the size of the list is index + 1.
 
santhi deeksha
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried the loop like Tom suggested and it worked!!! I should have thought of it! But thanks Shankar and Tom for taking the time to help me out. You guys are awesome.


 
Tom Rispoli
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
glad to help.
 
Shankar Tanikella
Ranch Hand
Posts: 329
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perfect
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic