• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help figuring out ClassCastException  RSS feed

 
Matt Senecal
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
  • 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?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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.
 
Dave Wingate
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!