Throws NPE on the following (slightly pathological :-) code snippet
ChoiceBox box = new ChoiceBox(items);
box.setSelectionModel(null);
box.setValue(items.get(2));
Beginning of StackTrace:
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at javafx.scene.control.ChoiceBox.lambda$new$134(ChoiceBox.java:113)
the code around line 113:
valueProperty().addListener((ov, t, t1) -> {
if (getItems() == null) return;
int index = getItems().indexOf(t1);
if (index > -1) {
// here we blow
getSelectionModel().select(index);
}
});
A couple of comments:
- either disallow null model or check for null _always_ (my preference would be the former, but then ...)
- that listener is duplicating logic: the invalidated method in value takes care of updating the selection - a quick fix might be to simply remove
Updated:
similar NPE in ComboBox - the listener that's installed in its contructor to valueProperty doesn't check for null
Affected code:
- ComboBox#setValue - no null check in listener added to valueProperty() (twice)
- ComboBox#setEditable - no null check in listener added to editableProperty()
- ComboBox#layout - no null check in ComboBoxListViewSkin.layoutChildren
- ChoiceBox#setValue - no null check in listener added to valueProperty()
ChoiceBox box = new ChoiceBox(items);
box.setSelectionModel(null);
box.setValue(items.get(2));
Beginning of StackTrace:
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at javafx.scene.control.ChoiceBox.lambda$new$134(ChoiceBox.java:113)
the code around line 113:
valueProperty().addListener((ov, t, t1) -> {
if (getItems() == null) return;
int index = getItems().indexOf(t1);
if (index > -1) {
// here we blow
getSelectionModel().select(index);
}
});
A couple of comments:
- either disallow null model or check for null _always_ (my preference would be the former, but then ...)
- that listener is duplicating logic: the invalidated method in value takes care of updating the selection - a quick fix might be to simply remove
Updated:
similar NPE in ComboBox - the listener that's installed in its contructor to valueProperty doesn't check for null
Affected code:
- ComboBox#setValue - no null check in listener added to valueProperty() (twice)
- ComboBox#setEditable - no null check in listener added to editableProperty()
- ComboBox#layout - no null check in ComboBoxListViewSkin.layoutChildren
- ChoiceBox#setValue - no null check in listener added to valueProperty()