java.lang.ClassCastException


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: java.lang.ClassCastException

  1. #1
    Join Date
    Nov 2006
    Posts
    2

    java.lang.ClassCastException

    I am trying to do the following and I am getting the exception:

    Node new_node=new Node(label);

    total_nodes.add(new_node);

    The total_nodes is of class TreeSet.

    And especially:

    at java.util.TreeMap.compare(TreeMap.java:1093)
    at java.util.TreeMap.put(TreeMap.java:465)
    at java.util.TreeSet.add(TreeSet.java:210)
    at ask4.Graph.insertVertex(Graph.java:42)

    I have done this only once when my tree is empty.But in the second time I get the exception.If anyone can help I would be grateful.

    Thanks.

  2. #2
    Join Date
    Nov 2006
    Posts
    35
    easy - first time it works, cause first element doesn't have to be compared. second time it fails, because the trees compare method is called. so what you have to do is to make your objects comparable - how exactly can be read in the documentation (which would have saved you a lot of time -> so god **** start to use it!). it says:

    Red-Black tree based implementation of the SortedMap interface....

    ...Note that the ordering maintained by a sorted map (whether or not an explicit comparator is provided) must be consistent with equals if this sorted map is to correctly implement the Map interface. (See Comparable or Comparator for a precise definition of consistent with equals.) This is so because the Map interface is defined in terms of the equals operation, but a map performs all key comparisons using its compareTo (or compare) method, so two keys that are deemed equal by this method are, from the standpoint of the sorted map, equal. The behavior of a sorted map is well-defined even if its ordering is inconsistent with equals; it just fails to obey the general contract of the Map interface.

    in the comparator interface document you find the following information:

    A comparison function, which imposes a total ordering on some collection of objects. Comparators can be passed to a sort method (such as Collections.sort) to allow precise control over the sort order. Comparators can also be used to control the order of certain data structures (such as TreeSet or TreeMap).

    and after 20 secs of googleing i found this: Objects that implement the Comparable interface can be inserted into sorted data structures in the API such as TreeMap.

    So guess what you have to do?!? right... implement the comparable interface.

    reading sometimes is faster than asking - especially when it comes to programming you should get used to it. hope i could help ;-)

Similar Threads

  1. EJB problem
    By vikassheelgupta in forum Java
    Replies: 4
    Last Post: 07-16-2005, 03:07 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center