• Post Reply Bookmark Topic Watch Topic
  • New Topic

HashMap throws ClassCastException  RSS feed

 
Sandip Chaudhuri
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I was using a HashMap and adding object to it.
I suddenly found it throwing a ClassCastException.
The error was somewhere inside HaspMap where it was trying to cast the object into Compareable and it was throwing class Cast exception.

I made the deduction that in the hashmap we cant add non caompareable objects.
But this is not true as other examples proved.

The Api says it
throws ClassCastException
if the class of the specified key or value prevents it from being stored in this map.

I cant figure out what values and key prevents them from being stored in the Map.
Plzz help.
 
Scott Selikoff
author
Bartender
Posts: 4093
21
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sandip Chaudhuri:
I made the deduction that in the hashmap we cant add non caompareable objects.


Recall that a Map is a set of Key Objects and Value Objects. When one typically talks about adding objects to a Map, he usually means adding Value Objects.

So to answer your first question, no you can store any Value Object of any class, there is no restriction.

The only conditional is on the Key Objects. Most of the time, developers tend to use Strings as keys since they are uniform, but there's nothing to stop you from using other, more complex, objects as keys.

Could you provide some sample code? It will probably indicate where the problem is.
 
Stuart Ash
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like you are trying to sort. Sort algorithms expect the elements to be comparable (by implementing Comparable). Please post code.
 
Sandip Chaudhuri
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am unable to post the code because i later changed it into an arrayLiist

the present Code is

private static List disableComponentChildren(Component comp, List disableList) {
if(comp instanceof JButton || comp instanceof JRLabel || comp instanceof JScrollBar )
{
return disableList;
}

if (!comp.isEnabled()) {
disableList.add(comp);

}
try {
if (((Container) comp).getComponentCount() > 0) {
comp.setEnabled(false);
Component[] components = ((Container) comp).getComponents();
int size = components.length;
for (int i = 0; i < size; i++) {
disableList =disableComponentChildren(components[i],disableList);
}
} else {
comp.setEnabled(false);
}
} catch (ClassCastException cee) {
comp.setEnabled(false);
}
return disableList;
}

unfortunately i cant change to Map to get the code to even complie because it affects alot

I wasnt sort or anything just adding.
I know that that it was a class cast exception and the Object was being cast into Compareable which led to my errorneous deduction.
What i want to know what type of values will actually throw the ClassCastException.
And i wasnt using string as the keys.
Thanks
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HashMap is almost certainly not throwing a ClassCastException.
Post the stack trace and offending code.
 
Stuart Ash
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sandip Chaudhuri:
I am unable to post the code because i later changed it into an arrayLiist



You don't use version control?



You must strongly consider using it.
 
Sandip Chaudhuri
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah i use version control but i found it was not working in the first instance so no uploading into cvs.

Will try and get the original somehow.
I thing i have got is any ordered Collection will need to be comparable like TreeSet.

But i still dont get what can throw a CalssCastException especially since Hashmap even allows NULL.

What i am wondering is actually why does it trrow a class cast exception.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have two theories:

First, that the exception was being thrown not by a Map, but by the explicit casts to Container in this piece of code. Why isn't the formal argument of type Container, since this routine only works with Containers?

The second theory is that you were using a TreeMap, not a HashMap. TreeMap will throw an exception if you store non-Comparables in it without specifying an explicit Comparator.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!