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.

5
  • Can you show your Node class? Is Node a type parameter by any chance? If yes, then this question will give you the answer Commented Feb 18, 2014 at 18:33
  • possible duplicate of How to: generic array creation Commented Feb 18, 2014 at 18:38
  • Consider showing enough code so that people can attempt to debug the issue. Commented Feb 18, 2014 at 18:38
  • Please give the exact error message with stack trace for the suggested line, it looks correct. Commented Feb 18, 2014 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. Commented Feb 18, 2014 at 19:16

3 Answers 3

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];
Sign up to request clarification or add additional context in comments.

2 Comments

Node[] array = new Node[initialSize]; causes this "generic array creation" error
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.
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[].

Comments

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];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.