Can't instantiate class (class cast exception) while using HashMap
I was able to create a selectManyListbox successfully when I used SelectItem in my backing bean this way:
publicclass FileManagementBean
{
private Object[] dataFile;
private UISelectMany dataFileItems;
public FileManagementBean()
{
dataFileItems =new UISelectMany();
dataFileItems.setId("fileItems" );
UISelectItems items =new UISelectItems();
items.setId("items" );
items.setValue( createItems() );
dataFileItems.getChildren().add(items);
}
private Object createItems(){
List files =new ArrayList();
SelectItem file =new SelectItem("file1","Data File No. 1");
files.add( file );
file =new SelectItem("file2","Data File No. 2");
files.add( file );
.......
.......
return files;
}
// some getters and setters
}
and below is the code to create the selectManyListbox:
<h:selectManyListbox id="fileItems"
binding="#{fileManagementBean.dataFileItems}"
value="#{fileManagementBean.dataFile}" required="true" size="14"/>
so far so good.
I got runtime error: cannot instantiate class, java.lang.ClassCastException when I tried to getKey and getValue from a HashMap. The reason that I start using HashMap is because a HashMap will be returned from accessing the database table. I first create a HashMap manually in the backing bean to test using a HashMap.
I do not see why I got the ClassCastException and need help. Here is my code (no compilation error):
// some import statements
publicclass FileManagementBean
{
private HashMap fNames;
private Object[] dataFile;
private UISelectMany dataFileItems;
public FileManagementBean()
{
fNames =new HashMap();
fNames.put("file1","Data File No. 1");
fNames.put("file2","Data File No. 2");
........
........
dataFileItems =new UISelectMany();
dataFileItems.setId("fileItems" );
UISelectItems items =new UISelectItems();
items.setId("items" );
items.setValue( createItems() );
dataFileItems.getChildren().add( items );
}
private Object createItems()
{
List files =new ArrayList();
SelectItem file =new SelectItem();
Set keys = fNames.keySet();
Iterator it = keys.iterator();
while ( it.hasNext() )
{
Entry entry = ( Entry ) it.next();
file.setLabel( entry.getValue().toString() );
file.setValue( entry.getKey() );
files.add( file );
}
return files;
}
// come getters and setters
}

