RFR: 6429812: NPE after calling JTable.updateUI() when using a header renderer + XP L&F [v6]

Alexey Ivanov aivanov at openjdk.java.net
Fri Jun 3 15:12:41 UTC 2022


On Fri, 3 Jun 2022 10:27:20 GMT, Tejesh R <tr at openjdk.org> wrote:

>> _Header_ object not initialized/set when paint() method of `WindowTableHeaderUI` class is executed. The paint() event is executed through explicit call of `JTable.updateUI()` in the regression test. In order to set the _header_ to the _called_ JTable, it is set in the `getTableCellRendererComponent()` method, which in turn makes the _header_ object available during paint event without causing NPE.
>
> Tejesh R has updated the pull request incrementally with one additional commit since the last revision:
> 
>   Simplified the Test

Changes requested by aivanov (Reviewer).

test/jdk/javax/swing/JTableHeader/TableRendererTest.java line 46:

> 44: import javax.swing.table.TableCellRenderer;
> 45: 
> 46: public class TableRendererTest{

A space between the name and opening brace, please.

test/jdk/javax/swing/JTableHeader/TableRendererTest.java line 50:

> 48:     static JFrame frame = null;
> 49:     private static JScrollPane jScrollPane = null;
> 50:     private static JTable table = null;

Assigning `null` explicitly is not required. A field will always be initialised to its default value, which is `null` for reference types.

Neither `jScrollPane` nor `table` are used outside of `initialize`, convert them to local variables.

test/jdk/javax/swing/JTableHeader/TableRendererTest.java line 61:

> 59:                     Thread.currentThread().setUncaughtExceptionHandler(new ExceptionCheck());
> 60:                     initialize();
> 61:                     //Adding the Test Frame to handle dispose

This comment is a left-over from previous version.

test/jdk/javax/swing/JTableHeader/TableRendererTest.java line 66:

> 64:                 }
> 65:             }
> 66:         });

The try-catch block is unnecessary, we expect no exception, please remove it.

I suggest using a lambda expression instead of anonymous class.

test/jdk/javax/swing/JTableHeader/TableRendererTest.java line 76:

> 74:         }
> 75:         System.out.println("Test Pass!");
> 76:         frame.dispose();

Dispose of the frame before you check for the status. Plus, you have to call dispose on EDT.

I suggest saving the caught exception and re-throwing it here directly or as the cause to `RuntimeException`.

Something like this:


        SwingUtilities.invokeAndWait(frame::dispose);

        if (exception.get() != null) {
            throw new RuntimeException("Test Case Failed. NPE raised!",
                                       exception.get());
        }
        System.out.println("Test Passed");


where `exception` is


    private static final AtomicReference<Throwable> exception =
            new AtomicReference<>();

test/jdk/javax/swing/JTableHeader/TableRendererTest.java line 84:

> 82:         table = new JTable(new MyModel());
> 83:         jScrollPane = new JScrollPane();
> 84:         jScrollPane.setBounds(new java.awt.Rectangle(0,0,350,618));

Suggestion:

        jScrollPane.setBounds(0, 0, 350, 618);

You can pass the parameters directly without creating `Rectangle`. Spaces after commas, please.

test/jdk/javax/swing/JTableHeader/TableRendererTest.java line 102:

> 100:     static class ExceptionCheck implements Thread.UncaughtExceptionHandler {
> 101:         public void uncaughtException(Thread t, Throwable e)
> 102:         {

Put the opening brace on the same line as the declaration, please.

Please add `@Override` annotation to this method and to methods in `MyModel` and `DecoratedHeaderRenderer`.

test/jdk/javax/swing/JTableHeader/TableRendererTest.java line 129:

> 127:         public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
> 128:             return render.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
> 129:         }

I suggest breaking these long lines. You can list parameters as in the interface declaration:
https://github.com/openjdk/jdk/blob/3789983e89c9de252ef546a1b98a732a7d066650/src/java.desktop/share/classes/javax/swing/table/TableCellRenderer.java#L94-L96

-------------

PR: https://git.openjdk.java.net/jdk/pull/8830



More information about the client-libs-dev mailing list