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

TableView with null selectionModel: throws NPE on sorting

    Details

      Description

      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);
          }

      }

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                fastegal Jeanette Winzenburg
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated: