How to identify the cause of ClassCastException?  
Author Message
www





PostPosted: 2007-11-21 23:52:00 Top

java-programmer, How to identify the cause of ClassCastException? 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.
 
Daniel Pitts





PostPosted: 2007-11-22 3:11:00 Top

java-programmer >> 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/>
 
lord.zoltar





PostPosted: 2007-11-22 3:24:00 Top

java-programmer >> How to identify the cause of ClassCastException? On Nov 21, 10:51 am, www <email***@***.com> 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.

You can always print the names of the object's classes with
someObject.getClass().getName();

Personally, I always find these problems are easier to solve when
stepping through with a debugger. Hopefully, you have that option.
 
 
www





PostPosted: 2007-11-22 3:47:00 Top

java-programmer >> How to identify the cause of ClassCastException? Thank you all.

Yes, I printed out "state.getClass().getName()" and it is type State. I
see what my problem is.

I have another related question. Suppose:

public class State
{
public Map<String, int> map = new Map<String, int>(); //please let me
use "public" here, the reason is to show my question below

private int numA;

public State()
{
setNumA(99);
}

public void setNumA(int a)
{
map.put("A", a);
}

}

public class WarmState extends State
{
private int numB;

public WarmState()
{
super();
setNumB(11);
}

public void setNumB(int b)
{
super.map.put("B", b);
}

}

With two classes above available, now:

State state = new State();

state.map.put("B", 33);

Can I cast state to type WarmState now?

Thank you.

 
 
Daniel Pitts





PostPosted: 2007-11-22 3:52:00 Top

java-programmer >> How to identify the cause of ClassCastException? www wrote:
> Thank you all.
>
> Yes, I printed out "state.getClass().getName()" and it is type State. I
> see what my problem is.
>
> I have another related question. Suppose:
>
> public class State
> {
> public Map<String, int> map = new Map<String, int>(); //please let
> me use "public" here, the reason is to show my question below
>
> private int numA;
>
> public State()
> {
> setNumA(99);
> }
>
> public void setNumA(int a)
> {
> map.put("A", a);
> }
>
> }
>
> public class WarmState extends State
> {
> private int numB;
>
> public WarmState()
> {
> super();
> setNumB(11);
> }
>
> public void setNumB(int b)
> {
> super.map.put("B", b);
> }
>
> }
>
> With two classes above available, now:
>
> State state = new State();
>
> state.map.put("B", 33);
>
> Can I cast state to type WarmState now?
>
> Thank you.
>
There are two concepts for type that I think you're confusing...
Runtime type and Compile-time type.

You can not change the runtime type of an object once it has been
created (new State() creates a State instance), Casting *only* changes
the compile-type type information (what the compiler sees).


--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
 
 
Lew





PostPosted: 2007-11-22 11:14:00 Top

java-programmer >> How to identify the cause of ClassCastException? Daniel Pitts wrote:
> There are two concepts for type that I think you're confusing...
> Runtime type and Compile-time type.
>
> You can not change the runtime type of an object once it has been
> created (new State() creates a State instance), Casting *only* changes
> the compile-type type information (what the compiler sees).

To state Daniel's point a little differently, in hopes that you can
triangulate on the concept, a variable has a compile-time type, and an object
itself has a run-time type. You can refer to an object via a variable of a
superclass (or super-interface) compile-time type.

Super something = new Sub();

For example,

List <String> data = new ArrayList <String> ();

List is a supertype of ArrayList. The object pointed to by the variable data
is still of type ArrayList, but the variable is of type List.

Now consider the inverse:

javax.management.AttributeList attributes = new ArrayList <Object> ():

Oops. You can't do that, because the object created by 'new' is a supertype
of AttributeList.

So therefore this will also fail:

ArrayList <Object> stuff = new ArrayList <Object> ();
javax.management.AttributeList attributes =
(javax.management.AttributeList) stuff;

Oops. Illegal cast.

<http://java.sun.com/javase/6/docs/api/javax/management/AttributeList.html>
<http://java.sun.com/javase/6/docs/api/java/util/ArrayList.html>

--
Lew
 
 
Roedy Green





PostPosted: 2007-11-22 14:06:00 Top

java-programmer >> How to identify the cause of ClassCastException? On Wed, 21 Nov 2007 10:51:40 -0500, www <email***@***.com> wrote, quoted
or indirectly quoted someone who said :

>ClassCastException

see
http://mindprod.com/jgloss/runerrormessages.html#CLASSCASTEXCEPTION
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com