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?
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
0

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 . . . ?
0
CEHJCommented:
Can you post your code?
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

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

0
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
0
krakatoaAuthor Commented:
That, iirc, was the first thing I tried - but let me double check that again. I fear it also flunked.
0
CEHJCommented:
What you tried was probably more like

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

Open in new window

0
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

0
krakatoaAuthor Commented:
Thanks. ;)
0
CEHJCommented:
:)
0
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
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.