Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Exception in thread "main" java.lang.ClassCastException: SuperClass cannot be cast to Subclass

  1. #1
    Junior Member Gerardgrundy's Avatar
    Join Date
    Nov 2012
    Posts
    15
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Exception in thread "main" java.lang.ClassCastException: SuperClass cannot be cast to Subclass

    I'm trying to print out the details from the subclass auctionsale but I'm getting:
    Exception in thread "main" java.lang.ClassCastException: SuperClass cannot be cast to Subclass the subclass extends the Superclass.

    I've been calling a method from the subclass.
    Am I on the right track or what should I be reading?
    I'm using
    Thanks in advance for your help.
    Regards
    Gerard

    ((SubClass)thing[i]).printDetails();


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Exception in thread "main" java.lang.ClassCastException: SuperClass cannot be cast to Subclass

    The compiler won't lie, so perhaps things are not subclassed the way you think they are.

    Post a brief example of code that generates that message. It would be useful to see the relevant things (the classes and that array) being declared.

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Exception in thread "main" java.lang.ClassCastException: SuperClass cannot be cast to Subclass

    For the sake of debugging your class hierarchy if it is really complex, try printing out the list of superclasses for the object in the array. To do that, add this bit of code before you cast it, and tell us what it prints out:
    Class C = thing[i].getClass();
    while (C != null) {
         System.out.println(C.getName());
         C = C.getSuperclass();
    }
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. Exception in thread "main" java.lang.NullPointerException problem.......
    By Adam802 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 20th, 2012, 02:23 AM
  2. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  3. Replies: 6
    Last Post: November 12th, 2010, 04:40 AM
  4. Replies: 2
    Last Post: March 26th, 2010, 11:22 AM
  5. Replies: 1
    Last Post: October 25th, 2009, 11:54 AM