populating SelectOneMenu using ArrayList

I have initiated an ArrayList which will hold the 50 US states in an Action class.

In the jsp page, the code goes like:

<h:selectOneMenu id="state" required="true" style="width:100%" value="#{memberSearchAction.state}">

<f:selectItems value="#{memberSearchAction.stateList}" />

</h:selectOneMenu>

In the Action class,

private String state =null;

private ArrayList stateList =new ArrayList();

publicvoid setStateList(ArrayList stateList){

stateList.add("Alabama");

stateList.add("Alaska");

stateList.add("Arizona");

....

}

public ArrayList getStateList(){

return stateList;

}

public String getState(){

return this.state;

}

publicvoid setState(String a_strState){

this.state = a_strState;

}

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

[1884 byte] By [aparna_ra] at [2007-11-27 9:05:07]
# 1

Did you call setStateList at any point?

The list would only be created if you called setStateList.

I think the following will work, though it might not be the best approach still.

private String state = null;

private ArrayList stateList = new ArrayList();

public void setStateList(ArrayList stateList) {

this.stateList = stateList;

}

public List getStateList() {

if (stateList == null){

stateList = buildStateList();

}

return stateList;

}

public String getState() {

return this.state;

}

public void setState(String a_strState) {

this.state = a_strState;

}

public List buildStateList(){

List stateList = new ArrayList();

stateList.add("Alabama");

stateList.add("Alaska");

stateList.add("Arizona");

return stateList;

}

evnafetsa at 2007-7-12 21:38:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Hi,I tried using your code. I receive the NullPointer Exception
aparna_ra at 2007-7-12 21:38:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...