wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes getting a class cast exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "getting a class cast exception" Watch "getting a class cast exception" New topic
Author

getting a class cast exception

Dorcas Rebanha
Greenhorn

Joined: Apr 04, 2007
Posts: 18
First, I create an ArrayList:

List agentList = new ArrayList();

Then I put some stuff into it. Then I try the following:

SortedSet agentSet = new TreeSet();
agentSet.addAll(agentList);

What should I do to avoid getting a ClassCastException?

Thanks for any help.

Dorcas
Vassili Vladimir
Ranch Hand

Joined: Mar 08, 2007
Posts: 1585
Hi,

Can you please provide a complete source code snippet ? What are you putting in the ArrayList exactly ?

Regards ...


Vassili ...
SCJP 5.0, SCWCD 1.4, SCJA 1.0
Dorcas Rebanha
Greenhorn

Joined: Apr 04, 2007
Posts: 18
Ummm, the agentList contains AgentDisplayObjects, and I don't know more than that. Somebody else wrote the code and I am just trying to understand it, and possibly fix that pesky ClassCastException.

Are you suggesting that the contents of agentList might the problem?

I originally thought simply that you couldn't addAll() an ArrayList into a TreeSet. So I was hoping for advice about getting around that, like maybe a different method to use to accomplish the purpose.

Dorcas
Vassili Vladimir
Ranch Hand

Joined: Mar 08, 2007
Posts: 1585


Please post a snippet of your code.

Thanks ...
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

TreeSet requires that the objects that you put into it implement interface Comparable.

You get this exception because your class AgentDisplayObject does not implement Comparable.

Note, if you can't modify class AgentDisplayObject, then you can use the constructor of class TreeSet that takes a Comparator as an argument (see the API documentation of class TreeSet). When you do that, your class AgentDisplayObject does not need to implement Comparable - the comparator that you pass to the constructor will be responsible for sorting AgentDisplayObject objects instead.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Dorcas Rebanha
Greenhorn

Joined: Apr 04, 2007
Posts: 18
Jesper! Thank you! You rock!

Dorcas
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Glad I could help...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getting a class cast exception
 
Similar Threads
ArrayList does it maintain order
error in optionsCollection tag: Failed to obtain specified collection
JDBCTemplate QueryForList returns wrong value
Getting data in Servlet and passing to jsp but could not display in jsp
question on session