Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8251481

TableCell accessing row: NPE on auto-sizing

XMLWordPrintable

        The example has a custom TableCell that uses the row's item to configure its own visuals. To reproduce the NPE, compile and run

        - note that the cells' content is not fully visible (no wonder, we set the column's pref too small ;)
        - double click the column's resize region to adjust its width
        - expected: column width extended to fit the cell content
        - actual: NPE thrown (and column width unchanged)

        Technically, the reason is that the row is actually null (due to the cell context not completely setup for measuring its size requirement JDK-8251480).

        This behavior is unexpected: client code shouldn't be required to bother with implementation details for certain use-cases but rely on a fully configured cell context if !cell.empty.

        The example:

            public class TableColumnAutoSizingNPE extends Application {
            
                private Parent createContent() {
                    TableView<String> table = new TableView<>(FXCollections.observableArrayList
                            ("short name", "and now really really longish name that exceeds all"));
                    TableColumn<String, String> name = new TableColumn<>("Name");
                    // just for control (without, it will throw initially)
                    name.setPrefWidth(100);
                    name.setCellValueFactory(cc -> new SimpleStringProperty(cc.getValue()));
                    name.setCellFactory(cc -> new TableCell<String, String>() {
            
                        @Override
                        protected void updateItem(String item, boolean empty) {
                            super.updateItem(item, empty);
                            if (empty) {
                                setText("");
                            } else {
                                // access item of tableRow:
                                // NPE on autosizing because row is null
                                setText(item + getTableRow().getItem());
                            }
                        }
                        
                    });
                    table.getColumns().addAll(name);
                    BorderPane content = new BorderPane(table);
                    return content;
                }
            
                @Override
                public void start(Stage stage) throws Exception {
                    stage.setScene(new Scene(createContent(), 400, 200));
                    //stage.setTitle(FXUtils.version());
                    stage.show();
                }
            
                public static void main(String[] args) {
                    launch(args);
                }
            
            }

              mhanl Marius Hanl
              fastegal Jeanette Winzenburg
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Created:
                Updated:
                Resolved: