ClassCastException

What does this mean ? :

Exception in thread "t2" java.lang.ClassCastException: [Ljava.lang.StackTraceEle
ment; cannot be cast to java.lang.StackTraceElement
LVL 17
krakatoaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
It means that you're trying to cast an array to a type that is an element of an array of that type

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
krakatoaAuthor Commented:
So iterating a collection of Map values ( via iterator.next() ) and trying to cast each object is not doing that . . . ?
CEHJCommented:
Can you post your code?
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

krakatoaAuthor Commented:
Yep . . .

Collection collection = mapOfThread.values();
									
it = collection.iterator();
																		
	while(it.hasNext()){
									
		//StackTraceElement ste = (StackTraceElement)it.next();
										//System.out.println(((StackTraceElement)(it.next())).getMethodName());
										
System.out.println(it.next());
									
}

Open in new window


The commented-out line is a runtime failure, but does compile. The present code both compiles and runs.

. . .  and  'mapOfThread' is taken to be :
Map mapOfThread = Thread.getAllStackTraces();

Open in new window

CEHJCommented:
You probably want

        while (it.hasNext()) {
            StackTraceElement[] elements = (StackTraceElement[]) it.next();

            for (StackTraceElement e : elements) {
                System.out.println(e.getMethodName());
            }
        }

Open in new window

but your problem might have been prevented by using generics
krakatoaAuthor Commented:
That, iirc, was the first thing I tried - but let me double check that again. I fear it also flunked.
CEHJCommented:
What you tried was probably more like

StackTraceElement element = (StackTraceElement) it.next();

Open in new window

krakatoaAuthor Commented:
Yeah, no I had the array casting in there . . . I realised that much at least. ;)

I think my error was, as you said firstly, using an element to cast the array.

So it now functions, per your advisory, but plus a loop to extract each E :

for(int a=0;a<elements.length;a++){System.out.println(elements[a].getMethodName());}

Open in new window

krakatoaAuthor Commented:
Thanks. ;)
CEHJCommented:
:)
krakatoaAuthor Commented:
(Interesting to see how one can see the native and non-native methods from that - which was the point of my exercise) ;0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.