This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes [closed] Understanding class casting exceptions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "[closed] Understanding class casting exceptions" Watch "[closed] Understanding class casting exceptions" New topic

[closed] Understanding class casting exceptions

Joseph Macer
Ranch Hand

Joined: Apr 20, 2008
Posts: 63
EDIT: I didn't solve this particular error, but when I "downgraded" my sending functions so that multithreading was not supported, I didn't get any of these errors. That means my multithreading functions don't have enough protection - so this bug trace was part of a larger problem.

Thread closed.


I'm having trouble understanding this stack trace and error message in my code. I am sending a lot of messages.ObjectMessage objects over a network. This object has an object packaged inside it, and an enum (type events.Events) to tell the client what sort of message it is.

Sometimes (not always) I get the following crash:

My main problem is I'm having trouble understanding that first line - "cannot assign instance of messages.ObjectMessage to field messages.BigMessage.event of type events.Events in instance of messages.ObjectMessage". Is it trying to assign an ObjectMessage to an events field? Confused :roll:

Thanks for any insight!
[ June 20, 2008: Message edited by: Joseph Macer ]
Jeff Storey
Ranch Hand

Joined: Apr 07, 2007
Posts: 230

In general, a ClassCastException is assigned (as you thought) when you try to assign one type of object to an object to which it is not compatible. for example,

What particularly is causing it in your case is difficult to tell without the exact code, but from your stack trace I can take some guesses here. You said you are sending ObjectMessages across a network. It would appear the client code tries to read the message and assign it to a BigMessage field. However, if the message being sent is not a BigMessage, this would cause the problem. Does that make sense?


Jeff Storey
Software Developer
Joseph Macer
Ranch Hand

Joined: Apr 20, 2008
Posts: 63
Yea, though in this case I'm pretty sure it was a problem with sending multiple objects. When I had a poorly-implemented multithreaded sending function, I got the above error occassionally. When I protected my output stream, I didn't. So in this case I think it was essentially a stream issue. If it ever pops up again, I'll know something else is amiss.
I agree. Here's the link:
subject: [closed] Understanding class casting exceptions
It's not a secret anymore!