views:

394

answers:

6

I'm presently refactoring a JTable which displays a multitude of different types of data. The primary reason for this refactoring is that there a few ClassCastExceptions (the author/friend who wrote the code is off on hiatus), and I can't seem to find where these are originating from. Due to the large codebase, I'm at a loss as to where to start. Does anyone have any suggestions? I realize and apologize for the ambiguity of this question!

I've included the stack trace below. Thanks!!

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Boolean
    at javax.swing.JTable$BooleanRenderer.getTableCellRendererComponent(Unknown Source)
    at javax.swing.JTable.prepareRenderer(Unknown Source)
    at javax.swing.plaf.basic.BasicTableUI.paintCell(Unknown Source)
    at javax.swing.plaf.basic.BasicTableUI.paintCells(Unknown Source)
    at javax.swing.plaf.basic.BasicTableUI.paint(Unknown Source)
    at javax.swing.plaf.ComponentUI.update(Unknown Source)
    at javax.swing.JComponent.paintComponent(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JComponent.paintToOffscreen(Unknown Source)
    at javax.swing.BufferStrategyPaintManager.paint(Unknown Source)
    at javax.swing.RepaintManager.paint(Unknown Source)
    at javax.swing.JComponent._paintImmediately(Unknown Source)
    at javax.swing.JComponent.paintImmediately(Unknown Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source)
    at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
+2  A: 

Probably the table contains a checkbox (when the column model states that the column contains type Boolean) and the renderer tries to convert the contents into a boolean. But probably the contents are just strings. The solution is to change the data in the table or to create your own renderer.

Bruno Ranschaert
Bruno,The contents weren't just Strings - which was my problem unfortunately. My column was containing all sorts of datatypes in addition to individual renderers. I didn't mention that explicitly, so that was my bad, but your idea would definitely have worked otherwise. Thanks for your help.
Chris Cowdery-Corvan
+1  A: 

I think the problem comes from your TableModel (jtable.getModel()) It said somewhere

(..)
public Class<?> getColumnClass(int column)
   {
   switch(column)
     {
   (...)
      case XX: return Boolean.class;
      }
   }

but the value in your model in this column is a String

public Object getValueAt(int row,int column)
  {
  (..)
   switch(column)
     {
   (...)
      case XX: return (a String);
      }
  }
Pierre
Yep, it was - this helped a lot. Thanks :)
Chris Cowdery-Corvan
A: 

To debug this problem, you may want to consider biting the bullet and putting a breakpoint in the JTable$BooleanRenderer.getTableCellRendererComponent() on the line that makes the cast

setSelected((value != null && ((Boolean)value).booleanValue()));

(from JTable.java 1.288 06/11/15)

and check the class type of value. when you find a String, you can identify the offending column and row from your model. That will at least give you a start on identifying the problem.

akf
This was very helpful in finding the culprit, thanks!
Chris Cowdery-Corvan
+1  A: 

This error which is occurring in BooleanRenderer is because it is expecting that the value that is from the table's model is of type Boolean and tries to cast to it (akf's answer has the exact line of code where it occurs).

My guess is that initially it was expected that the model would return Boolean values for the given column but at one point it is returning strings instead. Therefore, I would concentrate on what model is being used for this given table (is it a custom model? Is it the default model where it is adding values to it?) and see where it may be getting a String instead of a Boolean.

Avrom
Avrom, you're absolutely right - thanks. If you have a moment/if you're interested, see my above comment to mmeyers on the cause of the issue.
Chris Cowdery-Corvan
A: 

Because the problem has now taken a slightly different tune, I feel that a new question might make more sense.

Thanks for all your help, it's extremely appreciated.

For those interested, the question is now located here.

Chris Cowdery-Corvan
A: 

Sorry to dig up an old question, but I ran into this issue myself & this post came up in a search and this is what I ran into.

I had JUnits tests fail (and actually throwing runtime exceptions) but I continued to run add/removes on my JTable (in the JUnit test) which put the GUI application in a bad state, and I would see the ClassCastException come up exactly as Chris had described.

So the "fix" for me was to make sure that all unit tests catch their exceptions and return failure instead of proceeding to run more unit tests.

madchedar0