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

TableView with null selectionModel throws NPE on sorting

XMLWordPrintable

      To reproduce:

      - run
      - click header
      - expected: data sorted
      - actual: NPE thrown

      Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
      at javafx.scene.control.TableView.sort(TableView.java:1508)
      at javafx.scene.control.TableView.doSort(TableView.java:1578)
      at javafx.scene.control.TableView.lambda$new$37(TableView.java:532)
      at javafx.scene.control.TableView$$Lambda$86/19352785.onChanged(Unknown Source)

      Similar to RT-37674, the options to fix are
      - either allow null as value and make sure that each and every internal access is guarded against null
      - or disallow and clearly document that it is not a valid value (and cleanup internals and remove all guards)

      /**
       * Issue TableView throws NPE on sorting column if selectionModel property value is null.
       *
       * @author Jeanette Winzenburg, Berlin
       */
      public class TableSortNPEIfNullSelectionModel extends Application {
          private final ObservableList<Locale> data =
                  FXCollections.observableArrayList(Locale.getAvailableLocales()
                          );

          private Parent getContent() {
              // instantiate the table with null items
              TableView<Locale> view = new TableView<Locale>(data);
              TableColumn<Locale, String> column = new TableColumn<>(
                      "Language");
              column.setCellValueFactory(new PropertyValueFactory<>("displayLanguage"));
              // either click on header for sorting
              view.getColumns().addAll(column);
              view.setSelectionModel(null);
              // or add column to sort order immediately
              //view.getSortOrder().add(column);
              Pane parent = new HBox(100);
              parent.getChildren().addAll(view);
              parent.setPadding(new Insets(20));
              return parent;
          }
          
          @Override
          public void start(Stage primaryStage) throws Exception {
              Scene scene = new Scene(getContent());
              primaryStage.setTitle(System.getProperty("java.version"));
              primaryStage.setScene(scene);
              primaryStage.show();
          }

          public static void main(String[] args) {
              launch();
          }
          
      }

            angorya Andy Goryachev
            fastegal Jeanette Winzenburg
            Votes:
            1 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported: