Showing posts with label classcastexception. Show all posts
Showing posts with label classcastexception. Show all posts

Monday, April 16, 2007

Still having trouble with ClassCastException?


Photo Sharing and Video Hosting at Photobucket
ClassCastException is still the most common search term that sends people to this blog, although "swim across the atlantic ocean" is about to overtake it thanks, I can only guess, to fans of Detroit Roller Derby (don't ask ...).

So to be helpful to those readers who are not from Michigan I thought I'd clarify things as much as I can, be warned though it isn't much.

Because Sun's spec doesn't enforce a requirement for ClassCastExceptions to show the classes of the objects which you are trying to cast (see this earlier post) it is normally only possible to make any sense of a ClassCastException by reading the source code, and you really need to attach a debugger and examine the object that your programme is trying to cast before you can start to figure out why it won't work.

If your object seems to be of the correct class the problem is probably related to ClassLoaders, this article explains succinctly why that happens.

Sometimes, if you're smart, you can make a smart guess.

Sometimes (more often if programmers will take my advice) the exception message will tell you, but don't count on it.


If you are seeing a ClassCastException being thrown by a product you have installed then you need to check its documentation.

If the documentation doesn't help then you need to get help from the people who wrote it.


Monday, January 08, 2007

ClassCastException - why so coy?


Well it seems that 23% of the people who read this blog read it because they searched for the term "classcastexception".

If you are reading this because you are having trouble with a ClassCastException I suggest you try searching for "troubleshooting classcastexception" or something like that instead, it might also be a good idea to add the name of the class or product which is causing your pain. You could also read some of my other posts on the subject here.

But I'm going to say this again anyway...

Why doesn't ClassCastException have a constructor takes two objects and outputs the message "Can't cast xx.xx.xx to yy.yy.yy at" so that we can find out what couldn't be cast to what simply by applying our reading skills to the message and not having to
a) read the stack trace and
b) attach a debugger just to find out what the wrong type was.

I see this bug registered with Sun, so obviously someone else cares too, but not that much because it has been in-progress since june 2004.

The last time I blogged about CCE I got a comment, but it wasn't wholly supportive it just said "Get any Desired College Degree, In less then 2 weeks.!"


Thursday, April 06, 2006

ClassCastException


Why can't the message from ClassCastException tell us what class couldn't be cast into what other
Class?

I've said it before, but it still does my head in so I'm saying it again..

I know that you can put this into your message, but how many times do *other peoples* exeptions have this information in them?
Come to think of it why didn't Sun make ClassCastException have a constructor with arguments for two classes and a message:

ClassCastException(Class from, Class into, String message)

I mean, this one thing would probably save billions of man hours worldwide as people wouldn't need to compile in extra debug output or step through their programmes in the debugger.


I know nothing, I'm not a fortune teller, and you'd be insane to think that I am. This disclaimer was cribbed from an email footer I once received. It is so ridiculous I had to have it for myself.

Statements in this blog that are not purely historical are forward-looking statements including, without limitation, statements regarding my expectations, objectives, anticipations, plans, hopes, beliefs, intentions or strategies regarding the future. Factors that could cause actual results to differ materially from the forward looking statements include risks and uncertainties such as any unforeseen event or any unforeseen system failures, and other risks. It is important to note that actual outcomes could differ materially from those in such forward-looking statements.

Danny Angus Copyright © 2006-2013 (OMG that's seven years of this nonsense)