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

}

[4414 byte] By [jiapei_jena] at [2007-10-2 8:36:22]
# 1
It looks like you are intending to iterate over fNames.entrySet() instead of fNames.keySet().
kcounsella at 2007-7-16 22:37:56 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Yes, you are right. Thanks for spotting the error. Happy New Year!
jiapei_jena at 2007-7-16 22:37:56 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
Glad to help!
kcounsella at 2007-7-16 22:37:56 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...