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

(Tree)TableView with null selectionModel: throws NPE on sorting

XMLWordPrintable

    • b12

        Setting a null selection model in TableView and TreeTableView produce NPE on sorting (and probably in some other situations) because the check for null is not done in several parts of the code (the original bug report talks about TreeTable). Setting a null selection model is a valid way to disable selection in a (tree)table.

        There is also a similar issue with ListView JDK-8279640.

        ---

        To reproduce: run the example and click into column header to sort -> throws NPE wtih stacktrace:

        Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
        at javafx.controls/javafx.scene.control.TableView.sort(TableView.java:1569)
        at javafx.controls/javafx.scene.control.TableView.doSort(TableView.java:1652)
        at javafx.controls/javafx.scene.control.TableView.lambda$new$0(TableView.java:545)

        reason seems to be a missing null check in 1569:

                final List<TablePosition> prevState = new ArrayList<>(getSelectionModel().getSelectedCells());


        The example:

        import java.util.Locale;

        import javafx.application.Application;
        import javafx.collections.FXCollections;
        import javafx.collections.ObservableList;
        import javafx.collections.transformation.SortedList;
        import javafx.scene.Parent;
        import javafx.scene.Scene;
        import javafx.scene.control.TableColumn;
        import javafx.scene.control.TableView;
        import javafx.scene.control.cell.PropertyValueFactory;
        import javafx.scene.layout.BorderPane;
        import javafx.stage.Stage;

        /**
         * TableView with null selectionModel: NPE on sorting
         */
        public class TableViewNPEWithoutSelectionModel extends Application {

            private Parent getContent() {
                ObservableList<Locale> data = FXCollections.observableArrayList(
                        Locale.getAvailableLocales());
                SortedList<Locale> sorted = new SortedList<>(data);
                TableView<Locale> table = new TableView<>(sorted);
                sorted.comparatorProperty().bind(table.comparatorProperty());
                table.setSelectionModel(null);
                TableColumn<Locale, String> countryCode = new TableColumn<>("CountryCode");
                countryCode.setCellValueFactory(new PropertyValueFactory<>("country"));
                table.getColumns().addAll(countryCode);
                BorderPane pane = new BorderPane(table);
                return pane;
            }

            @Override
            public void start(Stage primaryStage) throws Exception {
                primaryStage.setScene(new Scene(getContent(), 800, 400));
                primaryStage.show();
            }
            
            public static void main(String[] args) {
                launch(args);
            }
        }

        edit:
        - setting a null model should also clear the existing selection in both Tree- and TableView, as well as focused index, since no more selection and cell/row focus is possible.

              angorya Andy Goryachev
              fastegal Jeanette Winzenburg
              Votes:
              0 Vote for this issue
              Watchers:
              6 Start watching this issue

                Created:
                Updated:
                Resolved: