2969

java.lang.ClassCastException: java.util.ArrayList cannot be cast to javax.faces.model.SelectItem [du

Question:

This question already has an answer here:

<ul><li> <a href="/questions/6848970/how-to-populate-options-of-hselectonemenu-from-database" dir="ltr" rel="nofollow">How to populate options of h:selectOneMenu from database?</a> <span class="question-originals-answer-count"> 5 answers </span> </li> </ul>

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

Answer1:

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.

Recommend

  • Show list in AlertDialog [duplicate]
  • Reading XML value to populate Java variable
  • Same values for every key in hashmap
  • Number of input rows in spark structured streaming with custom sink
  • How to update TableView items outside JavaFX Thread
  • How to make Observable that emit character after 1 second of time interval
  • Plotting Line Graph with JSON data, Android
  • Showing spaces between bars in android
  • Not receiving the complete JSON response
  • Dynamic listview save state
  • A good way to store/read a large amount of strings?
  • How to read stringified JSON string in Java [duplicate]
  • error: cannot access Task class file for bolts
  • Using GSON to parse Json into a JAVA Object where the Json Elements may vary
  • SimpleXML parsing elementlist exception
  • Why spring test is fail, does not work @MockBean
  • How to return JSON from spring RESTful service and access it using RestTemplate class
  • Navigation drawer is always inflated
  • Make a TextView inside a ListView use the default ListView row style
  • fromJson raises unparseable date exception
  • JPS useBean with HashMap
  • Sorting a HashMap, while keeping duplicates
  • How to apply async task into this
  • drawing random circles, storing their coorindates in an array
  • Primefaces ManyCheckbox inside ui:repeat calls setter method only for last loop
  • JTable with a ScrollPane misbehaving
  • Angular 2 constructor injection vs direct access
  • Java static initializers and reflection
  • Android Google Maps API OnLocationChanged only called once
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • Django query for large number of relationships
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?
  • failed to connect to specific WiFi in android programmatically
  • UserPrincipal.Current returns apppool on IIS
  • How to push additional view controllers onto NavigationController but keep the TabBar?
  • How can I use threading to 'tick' a timer to be accessed by other threads?
  • How do I use LINQ to get all the Items that have a particular SubItem?