File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getting a class cast exception

 
Dorcas Rebanha
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1585
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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

Regards ...
 
Dorcas Rebanha
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1585
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Please post a snippet of your code.

Thanks ...
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dorcas Rebanha
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper! Thank you! You rock!

Dorcas
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic