Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JTable strange behavior from getAccessibleChild() method resulting in null pointer in client code

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 ?

like image 235
Guillaume Avatar asked Oct 27 '25 15:10

Guillaume


1 Answers

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.

like image 171
Devon_C_Miller Avatar answered Oct 29 '25 05:10

Devon_C_Miller



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!