Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to populate values in a <s:select> tag from database?

 
Jyothsna Panchagnula
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have to retrieve list of statesfrom the database and populate it in the drop down on load of the page.

Sample Jsp page


sample Action class


sample UserRegistrationActionform


I see the following error when the page is loaded
java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
java.util.ArrayList.RangeCheck(Unknown Source)
java.util.ArrayList.get(Unknown Source)

Can any one tell me where am I going wrong

Thanks,
Jyothsna
 
Bilal A. Siddiqui
Ranch Hand
Posts: 53
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi jyotsna,

It seems your indexoutofbound exception is coming due to the declaration of your index=1. Make it index=0 and it will run fine. If this part is running fine we can move forward with populating issue.

Cheers !!!
Bilal
 
Jyothsna Panchagnula
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bidal,


I did change the index from 0 to 1 as it was giving out of bounds exception.
I am not able to figure out whats wrong with the code

is there any other way to populate the data into a drop down from java class.



thanks,
Jyothsna
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What version of Struts are you actually using? Your subject says "<s:select>", which is Struts 2, while all your code implies Struts 1.

I'm a bit confused on some of your code:Note that I've gone ahead and made the code readable--it's nice to take care when posting and make sure that it has posted the way you expect it to.

Why do you create empty array lists then immediately assign new values? There's no reason to create the list if you just overwrite the value a line or two later.

If your array is of size 1 then there is no element at index 1--Java indexes arrays and lists starting with index 0.

The appendList() method is creating a list of ActionForms--this is almost *certainly* not what you want, unless I'm completely misunderstanding the code--in which case the code should probably be cleaned up to better express its intent.

Your stack traces would be more valuable if (a) they were complete, and (b) you were compiling with debug information so it would include the line number of where the exception took place--pointing to the middle of Java code doesn't help us determine where *your* code is at fault.

Your JSP fragment involves a "stateList" while your Java fragment shows a "countryList"--this is an additional layer of confusion. Please narrow down the problem to one or the other and post the current code, properly escaped, with more useful error information.s
 
Shailesh Narkhede
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you use Iterater on ArrayList simply,
Instead of writing separate logic.
That is better I guess.


 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic