• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Help figuring out ClassCastException

 
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I inherited an unfinished (undocumented and uncommented) program from another programmer (now gone), and I'm trying to figure out a ClassCastException that gets thrown at runtime.

In the program contains a lot of custom classes for table manipulation. The two key classes here are TableIndex, and TableSorter. Their inheritance hierarchy is as follows:

TableSorter extends TableMap
TableMap extends AbstractTableModel
AbstractTableModel implements TableModel

TableIndex implements TableModel.

At some point, a JTable is created that has a TableIndex as it's model:



Later on, this call is made, and it throws a ClassCastException:



table.getModel() returns a TableIndex, which is being (illegally, apparently) cast to a TableSorter. Looking up the inheritance chain, both TableIndex and TableSorter are derived from TableModel. Why, then, is this not working? Is this problem because both classes are not extended from the same superclass? Can someone tell me where the break is?
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both derive from TableModel, so you could cast either TableSorter or TableIndex to a TableModel.

but you can't cast a TableIndex to a TableSorter.

if i had Chihuahua derives from Dog derives from Mammal, and Cat derives from Mammal, i can't cast a Cat as a Dog.
 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you post the exception you're getting?

Did you verify that getModel() is actually returning an object of type TableIndex?

e.g. System.out.println(table.getModel().getClass().getName());
 
Matt Senecal
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Yes I verified it. The exception is just your generic ClassCastException, specifying the line number and the file that the exception is being thrown in. No other information is being given.

Originally posted by Dave Wingate:
Could you post the exception you're getting?

Did you verify that getModel() is actually returning an object of type TableIndex?

e.g. System.out.println(table.getModel().getClass().getName());

 
Matt Senecal
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks! I just slapped my forehead and said "duh". I'm running on empty today....

Originally posted by fred rosenberger:
Both derive from TableModel, so you could cast either TableSorter or TableIndex to a TableModel.

but you can't cast a TableIndex to a TableSorter.

if i had Chihuahua derives from Dog derives from Mammal, and Cat derives from Mammal, i can't cast a Cat as a Dog.

 
Self destruct mode activated. Instructions for deactivation encoded in this tiny ad.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic