1

I am implementing my own priority queue and using a class called sportsball that uses it. The priority queue is based on generics and uses a Node (T object, int value) (aka Name of player and their score). I am getting a class cast exception error when I try to run the program.

Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [LPriorityQueue$Node;
    at PriorityQueue.<init>(PriorityQueue.java:35)
    at sportsball.main(sportsball.java:48) 

The lines in question are: PriorityQueue.java:35:

Node[] array = (Node[])(new Object[initialSize]);

sportsball.java:48:

PriorityQueue<String> queue = new PriorityQueue<String>(start, step);

Thank you for your help!

Note:

When I tried having line PriorityQueue.java:35:

Node[] array = new Node[initialSize];

the error: generic array creation pops up instead.

  • Can you show your `Node` class? Is `Node` a type parameter by any chance? If yes, then [this question will give you the answer](http://stackoverflow.com/q/18581002/1679863) – Rohit Jain Feb 18 '14 at 18:33
  • possible duplicate of [How to: generic array creation](http://stackoverflow.com/questions/529085/how-to-generic-array-creation) – DwB Feb 18 '14 at 18:38
  • Consider showing enough code so that people can attempt to debug the issue. – DwB Feb 18 '14 at 18:38
  • Please give the exact error message with stack trace for the suggested line, it looks correct. – Carsten Hoffmann Feb 18 '14 at 18:56
  • You have to create an array whose real element type matches the lower bound of your `Node` type parameter. Then you can cast it to `Node[]` but of course you have to care to keep that array internal to your queue implementation to avoid further heap pollution. – Holger Feb 18 '14 at 19:16

3 Answers3

2

You cannot cast an Object to a Node, so you shouldn't be able to cast an Object[] to a Node[]. Just create the Node[] directly.

Node[] array = new Node[initialSize];
rgettman
  • 167,281
  • 27
  • 248
  • 326
  • Node[] array = new Node[initialSize]; causes this "generic array creation" error – user3324715 Feb 18 '14 at 18:31
  • Your original error message led me to conclude that `Node` was a class, not a generic type parameter. In that case, try [How to: generic array creation](http://stackoverflow.com/questions/529085/how-to-generic-array-creation). – rgettman Feb 18 '14 at 18:34
0

You cannot cast object to the Node as you did

Node[] array = (Node[])(new Object[initialSize]);

hence the exception ClassCastException: [Ljava.lang.Object; cannot be cast to [LPriorityQueue$Node which is clear that object cannot be castable to Node Type

So you can go with this option

Node[] array = new Node[initialSize];
Girish
  • 1,667
  • 1
  • 16
  • 28
0

You cannot simply cast an Object[] to a Node[]. This will indeed generate a ClassCastException. Just create a Node[] instead (Node[] array = new Node[initialSize];). Without more code than this, I cannot say anything else about it.

Note that the message of the ClassCastException is already indicating the problem. [Ljava.lang.Object; refers to Object[] and similarly, [LPriorityQueue$Node; refers to Node[].

Chronio
  • 747
  • 3
  • 8