Basically, table sorting doesn't seem able to handle a null value of the table's items property. Couldn't find any documentation about whether or not a null value is allowed, so the bug is either
- null allowed: implementation error in default sort policy, which hands over the items as returned from getItems without checking if it is null
- null not allowed: should be specified in setItems/constructor and fail-fast with an NPE
Seeing the implemenation at other places (like: " // we quite happily accept items to be null here" in the constructor and all/most? other collaborators checking against null), I assume the first (and personally, I am quite happy if that indeed were the intention). There are good arguments either way, though, just should be clearly specified.
To reproduce:
- run the example below
- click on header
expected: do nothing (except changing the header icon)
actual: throws NPE
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at java.util.ArrayList.<init>(ArrayList.java:168)
at javafx.collections.FXCollections.sort(FXCollections.java:672)
at javafx.scene.control.TableView$3.call(TableView.java:467)
at javafx.scene.control.TableView$3.call(TableView.java:431)
at javafx.scene.control.TableView.sort(TableView.java:1430)
at javafx.scene.control.TableView.doSort(TableView.java:1460)
at javafx.scene.control.TableView.access$100(TableView.java:354)
at javafx.scene.control.TableView$4.onChanged(TableView.java:532)
Runnable example:
/**
* Issue TableView throws NPE on sorting column if items property value is null.
*
* @author Jeanette Winzenburg, Berlin
*/
public class TableSortNPEIfNullItems extends Application {
private Parent getContent() {
// instantiate the table with null items
TableView<String> view = new TableView<String>(null);
TableColumn<String, String> column = new TableColumn<>("Items");
// either click on header for sorting
view.getColumns().addAll(column);
// 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();
}
}
- null allowed: implementation error in default sort policy, which hands over the items as returned from getItems without checking if it is null
- null not allowed: should be specified in setItems/constructor and fail-fast with an NPE
Seeing the implemenation at other places (like: " // we quite happily accept items to be null here" in the constructor and all/most? other collaborators checking against null), I assume the first (and personally, I am quite happy if that indeed were the intention). There are good arguments either way, though, just should be clearly specified.
To reproduce:
- run the example below
- click on header
expected: do nothing (except changing the header icon)
actual: throws NPE
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at java.util.ArrayList.<init>(ArrayList.java:168)
at javafx.collections.FXCollections.sort(FXCollections.java:672)
at javafx.scene.control.TableView$3.call(TableView.java:467)
at javafx.scene.control.TableView$3.call(TableView.java:431)
at javafx.scene.control.TableView.sort(TableView.java:1430)
at javafx.scene.control.TableView.doSort(TableView.java:1460)
at javafx.scene.control.TableView.access$100(TableView.java:354)
at javafx.scene.control.TableView$4.onChanged(TableView.java:532)
Runnable example:
/**
* Issue TableView throws NPE on sorting column if items property value is null.
*
* @author Jeanette Winzenburg, Berlin
*/
public class TableSortNPEIfNullItems extends Application {
private Parent getContent() {
// instantiate the table with null items
TableView<String> view = new TableView<String>(null);
TableColumn<String, String> column = new TableColumn<>("Items");
// either click on header for sorting
view.getColumns().addAll(column);
// 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();
}
}