aspose file tools*
The moose likes Java in General and the fly likes java.lang.ClassCastException: Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "java.lang.ClassCastException:" Watch "java.lang.ClassCastException:" New topic
Author

java.lang.ClassCastException:

Elena Zarkova
Greenhorn

Joined: Feb 18, 2006
Posts: 2
Hi, everyone, first let me explain I'm new to forums so if I'm doing something wrong - sorry. Here's my question:
On the following line of code
LinkedList<DecoratedNode>[] dial = (LinkedList<DecoratedNode>[]) new Object[C+1];
I get java.lang.ClassCastException: [Ljava.lang.Object;
I'm actually trying to create an array of size C+1 of linked lists, and the linked lists have objects of type DecoratedNode as elements. The code actually compiles, but it gives that error message when I try to run the JUnit test for it.
If anyone has an idea why it's not working please help. Thank you!
P.S. When I try to do
LinkedList<DecoratedNode>[] dial = new LinkedList<DecoratedNode>[C+1];
I get the error message "generic array construction"
Joni Salonen
Ranch Hand

Joined: Jan 07, 2006
Posts: 53
> P.S. When I try to do
> LinkedList<DecoratedNode>[] dial = new LinkedList<DecoratedNode>[C+1];
> I get the error message "generic array construction"

That is the way you should do it. It's not an error but a warning and you can ignore it. The first way with the cast is simply wrong; it's like writing:
If you want, you can do this:


The reason why you get the warning message is that arrays are inherently "dangerous" in that they provide absolutely no type safety -- including when you use generics. The warning is given to discourage you from using arrays because it's a bad idea. You could achieve the same thing safely using e.g. an ArrayList instead:

[ February 18, 2006: Message edited by: Joni Salonen ]
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296


Actually you can't do that. The compiler will not let you create an array using generics. That "generic array construction" message is an error not a warning.
[ February 18, 2006: Message edited by: Garrett Rowe ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Joni Salonen
Ranch Hand

Joined: Jan 07, 2006
Posts: 53
Ah, thanks, I wrote it without checking. Lesson of the day: never assume.
But you quoted the wrong bit of code didn't you? What you can't write is "new LinkedList<DecoratedNode>[C+1]", the bit you quoted does compile (naturally with a warning).
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Originally posted by Joni Salonen:
Ah, thanks, I wrote it without checking. Lesson of the day: never assume.
But you quoted the wrong bit of code didn't you? What you can't write is "new LinkedList<DecoratedNode>[C+1]", the bit you quoted does compile (naturally with a warning).


That bit of code won't compile. The compiler won't let you specify any generics in the creation of an array.

LinkedList<String>[] array1 = new LinkedList<String>[10];//won't compile
LinkedList[] array2 = new LinkedList[10];//okay
LinkedList<?>[] array3 = new LinkedList<?>[10];//also okay
Joni Salonen
Ranch Hand

Joined: Jan 07, 2006
Posts: 53
what you quoted uses "new LinkedList[C+1]" which does compile but never mind..
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Originally posted by Joni Salonen:
what you quoted uses "new LinkedList[C+1]" which does compile but never mind..


Yeah, but you can't cast it to a genericly typed array. That cast won't work
Joni Salonen
Ranch Hand

Joined: Jan 07, 2006
Posts: 53
Hmmm, this is curious. Which version of Java are you using? It does compile on Sun's 1.5.0_04-b05.
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
You're right, I had a syntax error in my test program. The cast indeed will work with a warning provided.
Elena Zarkova
Greenhorn

Joined: Feb 18, 2006
Posts: 2
Thank you all for the answers and the help, but I still have a problem
I tried that
and that and they both work and don't give any more errors but now when I try to access dial[0], for example dial[0].add(something) or dial[0].size(), I get NullPointerException. It's like that LinkedList has never been initialized. Any ideas? Thank you!
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
When you declare an array of objects, you can think of it as declaring a container that will hold some objects that you will declare later. All objects in the array are initially set to null.

Look at this code

Garrett
[ February 19, 2006: Message edited by: Garrett Rowe ]
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608

The compiler won't let you specify any generics in the creation of an array.

Yes it will - as long as you use the unbounded wildcard.


Tony Morris
Java Q&A (FAQ, Trivia)
 
Consider Paul's rocket mass heater.
 
subject: java.lang.ClassCastException: