Anuradha Prasanna wrote:In the above program, i have used Comparator to sort DDog objects by name.
How to convert the Object o in line 1 to DDog dog?
DDog dog = (DDog) o; // gives a ClassCastException
The toArray() method (that takes no parameters) returns an Object array. Period. It is not an DDog array and hence, can't be casted.
If you want the Set to return a DDog array, use the other toArray() method, the one that has been overloaded to take an array.
Are you sure?? You'll get a NullPointerException in the loop, as the dArray will contain only 6 elements and the rest of the 14 elements will be null (I'm assuming that you used the same code for creating the TreeSet as in your first code)...
You will not get NullPointerException at line 2 because you have added 6 objects to TreeSet and then you are initializing an array of 20 object, so because only first 6 places will be filled with objects and rest will be filled with NULL,
Joined: Mar 09, 2006
thanks harpreet, i got it now!
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
subject: ClassCastException when converting Object to DDog