Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

populating SelectOneMenu using ArrayList

 
Aparna Ram
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • 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
 
chloe wong
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • 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;
}
 
Freddy Wong
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
Chloe Wong, I tried using your code, but I receive the NullPointer Exception.. I dont understand why though
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic