java.lang.ClassCastException: java.util.ArrayList cannot be cast to javax.faces.model.SelectItem
11,830
You have to use f:selectItems
:
<h:selectOneMenu value="#{loginBean.dropDownValue}">
<f:selectItems value="#{loginBean.testDropDown}"/>
</h:selectOneMenu>
f:selectItem
is used to add a single SelectItem
to the menu, to add a whole List
of SelectItems
you have to use this tag.
Related videos on Youtube
Author by
user2727493
Updated on June 21, 2022Comments
-
user2727493 almost 2 years
I am trying to make my dropdown dynamic and below is the code and exception i am getting. Please help me out
bean:
private String dropDownValue; List<SelectItem> testDropDown = new ArrayList<SelectItem>(); List<SelectItem> testDropDownTwo = new ArrayList<SelectItem>(); public String getDropDownValue() { return dropDownValue; } public void setDropDownValue(String dropDownValue) { this.dropDownValue = dropDownValue; } public List<SelectItem> getTestDropDown() { testDropDown.add(new SelectItem("One")); testDropDown.add(new SelectItem("Two")); testDropDown.add(new SelectItem("Three")); testDropDown.add(new SelectItem("Four")); return testDropDown; }
XHtml Code:
<h:selectOneMenu value="#{loginBean.dropDownValue}"> <f:selectItem value="#{loginBean.testDropDown}" /> </h:selectOneMenu>
Exception:
exception
javax.servlet.ServletException: java.util.ArrayList cannot be cast to javax.faces.model.SelectItem javax.faces.webapp.FacesServlet.service(FacesServlet.java:321) root cause java.lang.ClassCastException: java.util.ArrayList cannot be cast to javax.faces.model.SelectItem com.sun.faces.renderkit.SelectItemsIterator.initializeItems(SelectItemsIterator.java:185) com.sun.faces.renderkit.SelectItemsIterator.hasNext(SelectItemsIterator.java:131) com.sun.faces.renderkit.html_basic.MenuRenderer.renderOptions(MenuRenderer.java:758) com.sun.faces.renderkit.html_basic.MenuRenderer.renderSelect(MenuRenderer.java:840) com.sun.faces.renderkit.html_basic.MenuRenderer.encodeEnd(MenuRenderer.java:294) javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:879) javax.faces.component.UIComponent.encodeAll(UIComponent.java:1650)
Some classcast exception is happening. but i am sending the ArrayList of SelectItem to the list
if i change the Value attribute in selectItem tag to itemValue it is not trowing a exception but i am not getting any value i am getting the object names as in the drop down in a single list
-
user2727493 over 10 yearsExcellent man thank u
-
kolossus over 10 years@user2727493 - still not going to accept an answer are we? You're risking users here ignoring your questions based on your acceptance history. Good luck