This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ClassCastException when converting Object[] to DDog[] 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 » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "ClassCastException when converting Object[] to DDog[]" Watch "ClassCastException when converting Object[] to DDog[]" New topic
Author

ClassCastException when converting Object[] to DDog[]

Anuradha Prasanna
Ranch Hand

Joined: Mar 09, 2006
Posts: 115

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


SCJP 6.0 90%
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18546
    
  40

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.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

Looking for this method ? -> http://java.sun.com/javase/6/docs/api/java/util/List.html#toArray(T[])


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Anuradha Prasanna
Ranch Hand

Joined: Mar 09, 2006
Posts: 115


when i use the above lines, to convet TreeSet<DDog> d into DDog[] using the other toArray() method, i get NullPointerException in line 2.
can anyone correct me?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

i get NullPointerException in line 2.

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)...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Harpreet Singh janda
Ranch Hand

Joined: Jan 14, 2010
Posts: 317

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,

Anuradha Prasanna
Ranch Hand

Joined: Mar 09, 2006
Posts: 115
thanks harpreet, i got it now!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ClassCastException when converting Object[] to DDog[]
 
Similar Threads
K&B chapter 7 question 15. Collections.
K & B - Generics & Collections
generics
Comparator Doubt
Illegal start of expression compilation error