Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ClassCastException

Status
Not open for further replies.

soldierdackel

Programmer
Jun 8, 2004
7
0
0
DK
Hi people,

I'm new to Java and therefore seek "professional" help. Try to take a look at below code:

DocumentAbbrMap d;
for (int i=0; i < arrayList.size(); i++) {
d = (DocumentAbbrMap) arrayList.get(i);
}

For some strange reason I get a ClassCastException when I do the above. I tried using Vector with the same result.

I also made sure that the ArrayList was actually populated with instances of type DocumentAbbrMap.

I don't get the exception if I add Objects to the ArrayList.

Please, anyone?
 
Check what is actually in the ArrayList by doing :

for (int i=0; i < arrayList.size(); i++) {
String className = arrayList.get(i).getClass().getName();
System.err.println(className);
}
 
Object o = arrayList.get(i)
d = null;
if(d instanceof DocumentAbbrMap)
d = (DocumentAbbrMap)o;

Ion Filipski
1c.bmp
 
Hi all,

It's all my mistake. For some strange reason I placed the class DocumentAbbrMap in two different packages. One of them was added to the ArrayList, and the other one was retrieved from the ArrayList.

Stupid stupid!

Cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top