aspose file tools*
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 EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "[closed] Understanding class casting exceptions" Watch "[closed] Understanding class casting exceptions" New topic
Author

[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.





Hello,

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
Joseph,

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


Jeff Storey
Software Developer
[url]http://jeffastorey.blogspot.com[/url]
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.
 
Consider Paul's rocket mass heater.
 
subject: [closed] Understanding class casting exceptions
 
Similar Threads
class loader problem while jboss clustering(Sorry If my post is being duplicated)
Logging problem log4j:ERROR
RMI classcast exception
Calling Entity Bean from Tomcat Servlet
SEVERE: javax.faces.FacesException: java.lang.ClassCastException: