• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

java.lang.ClassCastException:

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> 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 ]
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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 ]
 
Joni Salonen
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what you quoted uses "new LinkedList[C+1]" which does compile but never mind..
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're right, I had a syntax error in my test program. The cast indeed will work with a warning provided.
 
Elena Zarkova
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic