views:

27

answers:

2

Hello,

I've encountered a strange behavior from JTable (JDK 1.5_22):
After a selection change in the table and under some unknown particular circumstances, the JTable will call the cell renderer with 'null' for the value parameter.
This will eventually lead to a nice 'Null Pointer Exception' on a custom renderer code that is not ready for such a rude call.

Here is the guilty method (JTable.java, line 5319) :

public Accessible getAccessibleChild(int i) {
            if (i < 0 || i >= getAccessibleChildrenCount()) {
                return null;
            } else {
                // children increase across, and then down, for tables
                // (arbitrary decision)
                int column = getAccessibleColumnAtIndex(i);
                int row = getAccessibleRowAtIndex(i);

                TableColumn aColumn = getColumnModel().getColumn(column);
                TableCellRenderer renderer = aColumn.getCellRenderer();
                if (renderer == null) {
                    Class<?> columnClass = getColumnClass(column);
                    renderer = getDefaultRenderer(columnClass);
                }
                Component component = renderer.getTableCellRendererComponent(
                                  JTable.this, null, false, false,
                                  row, column);
                return new AccessibleJTableCell(JTable.this, row, column,
                      getAccessibleIndexAt(row, column));
            }
        }

and here is a focus on the faulty statement:

Component component = renderer.getTableCellRendererComponent(
                                  JTable.this, null, false, false,
                                  row, column);

Asking google whith "JTable getAccessibleChild 5334" was interesting: I'm not alone to encounter this 'feature'. But there were no answer.

Most well formulated question is located on official sun forum.

Does anyone have a clue about this ?

A: 

Anytime I see otherwise inexplicable problems like this, I wonder about incorrect synchronization. For example,

1) Failing to construct the component on the event dispatch thread.

2) Mutating the component's model on some other thread.

Violations manifest more frequently in the presence of complex initialization, a model with unexpected latency, or different hardware. There may also be a bug, but these two points are worth checking.

trashgod
+2  A: 

It's not a synchronization or EDT issue. The code in JTable is explicitly calling getTableCellRendererComponent with a null value.

The value returned is never used, so, on the surface, it rather looks like old debugging code. However I suspect it is there to not break code that expects getTableCellRendererComponent to be called before a cell is accessed.

Sun has been called on this issue before and their answer was that the API makes no guarantees that value is non-null, so getTableCellRendererComponent must fail gracefully when called with a null.

Devon_C_Miller
+1 Well spotted.
trashgod
I like the 'old debugging code' theory :D
Guillaume