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

[ChoiceBox, ComboBox] throws NPE on setting value on null selectionModel

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • jfx18
    • 8u20
    • javafx

      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()

            mhanl Marius Hanl
            fastegal Jeanette Winzenburg
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported: