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

TableCell: NPE on modifying item's list

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • P4
    • jfx19
    • 8u261, 9.0.4, jfx14
    • javafx

    Backports

      Description

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

        - press the button to modify the items list (note: the type of modification doesn't matter)
        - expected: modification updates the table without error
        - actual: throws NPE

        Technically, the error is that at some time during the update process the row is empty (aka: item == null) while cell is not empty. Both should be in sync always, just the same as for the sync of the item (see: fixed JDK-8115269): that is either message the cell update only if the row item is complete or implement the cell update to regard itself as empty if the row is empty.

        The example:

            public class TableCellRowItemNPE 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");
                    // workaround JDK-8251481
                    name.setPrefWidth(200);
                    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 a property of row's item
                                // NPE because item is null during update after items modification
                                setText(item + getTableRow().getItem().toString());
                            }
                        }
            
                    });
                    table.getColumns().addAll(name);
                    
                    Button add = new Button("add item");
                    add.setOnAction(e -> table.getItems().add("added"));
                    BorderPane content = new BorderPane(table);
                    content.setBottom(new HBox(10, add));
                    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);
                }
            
            }

        Attachments

          Issue Links

            Activity

              People

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

                Dates

                  Created:
                  Updated:
                  Resolved: