Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2007

Tip: Looking for answers? Try searching our database.

How to identify the cause of ClassCastException?

Thread view: 
www - 21 Nov 2007 15:51 GMT
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.
Gordon Beaton - 21 Nov 2007 15:44 GMT
> 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.

Obviously your state object wasn't a WarmState. So catch the exception
and display e.g. state.getClass().getName() to see the object's actual
class.

/gordon

--
Mark Rafn - 21 Nov 2007 19:06 GMT
>I have two classes: State and WarmState.
>public class State
[quoted text clipped - 5 lines]
>    ... //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.

Why did you guess?  In general, you should keep the specific type if you
need it to be specific, so you can't make this mistake.  If you need something
that may or may not be a WarmState, then checking with the instanceof operator
before the cast is a good idea.  Or use the Visitor pattern
(http://en.wikipedia.org/wiki/Visitor_pattern) to dispatch to the
subclass-specific handler.

>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:

In jdk1.5 and newer (maybe jdk1.4, I can't remember), the message for the
ClassCastException should include the type of the object on which the cast
was attempted.  Older versions didn't, and you have to put it under a debugger
or add debugging code.

>e.getMessage()

If this doesn't give you something like: "your.package.State", you're running
an old VM, or something else is strange.

The following code:
 public class ClassCast {
   public static void main(String[] args) throws Exception {
       Object o = new Object();
       try {
           System.out.println((String)o);
       } catch (Exception e) {
           System.out.println(e.getMessage());
       }
   }
 }
shows this for me:
 [dagon tmp]$ java -version
 java version "1.5.0_12"
 Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_12-b04)
 Java HotSpot(TM) Server VM (build 1.5.0_12-b04, mixed mode)
 [dagon tmp]$ java -cp . ClassCast
 java.lang.Object
--
Mark Rafn    dagon@dagon.net    <http://www.dagon.net/>
Daniel Pitts - 21 Nov 2007 19:10 GMT
> Hi,
>
[quoted text clipped - 23 lines]
>
> 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?

Signature

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

lord.zoltar@gmail.com - 21 Nov 2007 19:24 GMT
> Hi,
>
[quoted text clipped - 25 lines]
>
> 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 - 21 Nov 2007 19:46 GMT
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 - 21 Nov 2007 19:52 GMT
> Thank you all.
>
[quoted text clipped - 48 lines]
>
> 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).

Signature

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

Lew - 22 Nov 2007 03:13 GMT
> 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>

Signature

Lew

Roedy Green - 22 Nov 2007 06:06 GMT
>ClassCastException

see
http://mindprod.com/jgloss/runerrormessages.html#CLASSCASTEXCEPTION
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.