class Cast exception >> Struts >> FormFile

private String resetFileList(FormFile currentFile, HashMap filesData){

Iterator it = filesData.keySet().iterator();

StringBuffer fileNames =new StringBuffer();

while (it.hasNext()){

FormFile tempFile = (FormFile) it.next();

fileNames.append(tempFile.getFileName()).append("\n");

}

return fileNames.toString();

}

I am using a html:file in my form. when the user submits the file for upload, in action I am storing the FormFile in to the session and reading adding the new file to the HashMap and setting the HashMap to the session again. This way I keep track of all FormFile objects and push them once, when the user submits the button.

I do not understand why I am getting class cast exception when I am reading the FormFile object from the HashMap i stored to the session.

Thanks in advance.

[1138 byte] By [Naga.Kadiyalaa] at [2007-10-3 10:02:58]
# 1
Print out the name of the class that is in the hashmap. This might give you a clue as to what the problem is.
zadoka at 2007-7-15 5:21:52 > top of Java-index,Java Essentials,Java Programming...
# 2
u mean .. u want me to print the class >>> obj.getClass()?
Naga.Kadiyalaa at 2007-7-15 5:21:52 > top of Java-index,Java Essentials,Java Programming...
# 3
> u mean .. u want me to print the class >>>> obj.getClass()?Sure. It must be something other than FormFile. If it comes back as a String, or something else then it would be helpful to know what it is.
zadoka at 2007-7-15 5:21:52 > top of Java-index,Java Essentials,Java Programming...
# 4

in your loop do this,

while (it.hasNext()) {

Object obj = it.next();

System.out.println(obj.getClass().getName());

FormFile tempFile = (FormFile) obj;

fileNames.append(tempFile.getFileName()).append("\n");

}

just noticed you are iterating over the keyset, not the values. Change that to get an iterator for the values

Iterator it = filesData.values().iterator();

No more editing!! I swear

Message was edited by:

SomeoneElse

SomeoneElsea at 2007-7-15 5:21:52 > top of Java-index,Java Essentials,Java Programming...
# 5
I am still getting the classcast exception and the calss name printed is:>>>>class name: [BSomething I do not understand.
Naga.Kadiyalaa at 2007-7-15 5:21:52 > top of Java-index,Java Essentials,Java Programming...
# 6
Does your code to print out the class name look like this:System.out.println(obj.getClass().getName());
zadoka at 2007-7-15 5:21:52 > top of Java-index,Java Essentials,Java Programming...
# 7

yes. Hereunder is my new code.

private String resetFileList(FormFile currentFile, HashMap formFileMap) {

Iterator it = formFileMap.values().iterator();

StringBuffer fileNames = new StringBuffer();

while (it.hasNext()) {

Object obj=it.next();

System.out.println("class name: "+obj.getClass().getName());

FormFile formFile=(FormFile)obj;

System.out.println("class name: "+formFile.getClass().getName());

System.out.println("File name >>" + formFile.getFileName() + ", File Size>> " + formFile.getFileSize());

fileNames.append(formFile.getFileName() + "\n");

}

return fileNames.toString();

}

getting exception at the line where I casting to the FormFile.

Naga.Kadiyalaa at 2007-7-15 5:21:52 > top of Java-index,Java Essentials,Java Programming...
# 8

Dear All

Thanks for all the postings.

I made a small mistake when I was setting the FormFile object. I made some changes after I posted the issue here. I was setting the bytes instead of the FormFile object. I corrected that all are working fine.

But I did what suggetsed by SomeOneElse to iterate against the values, instead of the keyset. That is the main problem I was getting classcast exception. Thanks to others who guided to debug the issue.

Cheers.. :)

Naga.Kadiyalaa at 2007-7-15 5:21:52 > top of Java-index,Java Essentials,Java Programming...