aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes I need Y the ClaaCastException is coming? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "I need Y the ClaaCastException is coming?" Watch "I need Y the ClaaCastException is coming?" New topic
Author

I need Y the ClaaCastException is coming?

Rajitha Murthy
Greenhorn

Joined: Mar 31, 2006
Posts: 4
hi,
the following program is generating classcastexcception.I need the reason for that and also i need how to catch that exception

import java.util.*;
public class Test1{
public static void main(String a[]) {
Set s = new TreeSet();
s.add(new Person(20));
s.add(new Person(10));
System.out.println(s);
}
}
class Person{
Person(int i){}
}
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Note the error message...

This tells you that the problem starts at line 6 of your Test1.java file, which is when you try to add the second element. If you check the API documentation for TreeSet, you will see that the add method throws a ClassCastException "if the specified object cannot be compared with the elements currently in the set."

Elements in a TreeSet are "sorted according to the natural order of the elements (see Comparable), or by the comparator provided at set creation time, depending on which constructor is used."

So the problem is that these elements (instances of Person) are not Comparable and no Comparator is provided to the set constructor.
[ April 13, 2006: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Rajitha Murthy:
...and also i need how to catch that exception...

In general, you catch exceptions by placing the code that might throw an exception inside a try block, and then follow the try block with a catch block that "handles" the exception...

There's a bit more to it, but that's a general idea of how to catch an exception.
subir rastogi
Greenhorn

Joined: Apr 11, 2006
Posts: 18
hi,i made some changes to your code and this is working now.You have override the method compareTo.....
Have a look at the below code that will give you better understanding.

import java.util.*;

public class Test1 {
public static void main(String a[]) {
try {
Set s = new TreeSet();
s.add(new Person(10));
s.add(new Person(20));
System.out.println(s);
} catch (ClassCastException e) {
e.printStackTrace();
}
}

}


class Person implements Comparable{
int j;
Person(int i) {
j=i;
}
public int compareTo(Object o) {
if((o instanceof Person)&&(((Person)o).j ==this.j))
return 0;
return 1;
}
}
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by subir rastogi:
hi,i made some changes to your code and this is working now.You have override the method compareTo...

Actually, compareTo should return a negative value if this object is "less than" the object it's being compared to; zero if they are equal, and a positive value if this object is "greater than" the object it's being compared to.

In this case, you could simply return the difference of Person's int value...

Also from the API for Comparator...
It is strongly recommended (though not required) that natural orderings be consistent with equals. This is so because sorted sets (and sorted maps) without explicit comparators behave "strangely" when they are used with elements (or keys) whose natural ordering is inconsistent with equals.

And finally, you will probably want to override the toSting method in Person, so that when you print your TreeSet you can verify the ordering.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: I need Y the ClaaCastException is coming?