Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

populating SelectOneMenu using ArrayList

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have initiated an ArrayList which will hold the 50 US states in an Action class.
In the jsp page, the code goes like:


In the Action class,


I would like the dropdown menu to be populated with the states. Will this code work? Where am I going wrong, as it does not get populated?

PS: I am a total newbie to the field of programming. Kindly help. Thanks
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public List getStateList()
{
List dropList= new ArrayList();
SelectItem selectItemForDropList = new SelectItem();
try
{
dropList.add(new SelectItem(String.valueOf("Alabama"),"Alabama"));
dropList.add(new SelectItem(String.valueOf("Alaska"),"Alaska"));
dropList.add(new SelectItem(String.valueOf("Arizona"),"Arizona"));

}
catch(Exception e)
{
//do something

}
return dropList;
}
 
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When adding the element into the list, you should add the SelectItem object, not the String, e.g.
stateList.add(new SelectItem(1, "Alabama"));
stateList.add(new SelectItem(2, "Alaska"));
... etc
 
chloe wong
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
another way to do it will be..

private selectStateList = new ArrayList();

public List getStateList()
{
selectStateList .clear();
SelectItem selectItem = new SelectItem();
selectItem.setValue("Alabama");
selectItem.setLabel("Alabama");
selectStatusList.add(selectItem);

selectItem = new SelectItem();
selectItem.setValue("Alaska");
selectItem.setLabel("Alaska");
selectStateList .add(selectItem);
return selectStateList ;
}
 
Aparna Ram
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chloe Wong, I tried using your code, but I receive the NullPointer Exception.. I dont understand why though
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic