javafx ComboBox <integer>的setValue(Integer)方法导致NullpointerException

时间:2017-10-17 09:36:01

标签: java user-interface javafx

我有一个ComboBox<Integer>,我想在其中设置初始选定值。我还有一个ChangeListener附加到selectedItemProperty:

this.cbPlayerCount = new ComboBox<>(observableArrayList(2, 3, 4));
cbPlayerCount.getSelectionModel()
             .selectedItemProperty()
             .addListener(this::playerCountChanged);

cbPlayerCount.setValue(2);

setValue方法的调用会触发propertyChangeListeners链(我的未包括)并最终抛出NullpointerException。 我的侦听器方法的签名如下所示:

private void playerCountChanged(ObservableValue<?> val, int old, int newVal)

但是从不调用它的代码。 stacktrace看起来像这样:

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(ExpressionHelper.java:361)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
at javafx.beans.property.ReadOnlyObjectPropertyBase.fireValueChangedEvent(ReadOnlyObjectPropertyBase.java:74)
at javafx.beans.property.ReadOnlyObjectWrapper.fireValueChangedEvent(ReadOnlyObjectWrapper.java:102)
at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:112)
at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:146)
at javafx.scene.control.SelectionModel.setSelectedItem(SelectionModel.java:102)
at javafx.scene.control.ComboBox$ComboBoxSelectionModel.lambda$new$154(ComboBox.java:494)
at com.sun.javafx.binding.ExpressionHelper$SingleInvalidation.fireValueChangedEvent(ExpressionHelper.java:137)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
at javafx.beans.property.ReadOnlyIntegerPropertyBase.fireValueChangedEvent(ReadOnlyIntegerPropertyBase.java:72)
at javafx.beans.property.ReadOnlyIntegerWrapper.fireValueChangedEvent(ReadOnlyIntegerWrapper.java:102)
at javafx.beans.property.IntegerPropertyBase.markInvalid(IntegerPropertyBase.java:113)
at javafx.beans.property.IntegerPropertyBase.set(IntegerPropertyBase.java:147)
at javafx.scene.control.SelectionModel.setSelectedIndex(SelectionModel.java:68)
at javafx.scene.control.SingleSelectionModel.updateSelectedIndex(SingleSelectionModel.java:215)
at javafx.scene.control.SingleSelectionModel.select(SingleSelectionModel.java:149)
at javafx.scene.control.SingleSelectionModel.clearAndSelect(SingleSelectionModel.java:103)
at javafx.scene.control.ComboBox.lambda$new$152(ComboBox.java:262)
at com.sun.javafx.binding.ExpressionHelper$SingleChange.fireValueChangedEvent(ExpressionHelper.java:182)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
at javafx.beans.property.ObjectPropertyBase.fireValueChangedEvent(ObjectPropertyBase.java:105)
at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:112)
at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:146)
at javafx.scene.control.ComboBoxBase.setValue(ComboBoxBase.java:150)
at de.dk.bm.menu.view.MenuView.<init>(MenuView.java:33)
...
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)

我使用的是jdk1.8.0_92。 没有错误消息或任何内容,只有异常。我尝试评论添加ChangeListener的代码,即使从未调用过该代码。如果没有附加监听器,则不会出现异常。但我仍然不知道为什么在添加监听器时抛出它。我不想调试框架代码来查找导致此问题的错误。为什么抛出此异常?这是javafx框架中的错误还是我错了?

1 个答案:

答案 0 :(得分:1)

错误在于我使用int类型作为playerCountChanged(Observable<?>, int, int)方法的参数。所以第一次选择一个值(通过{{1}的调用}方法),没有先前选择的值,因此作为参数setValue传递的值为int old。由于我使用null代替int,因此java会尝试将Integer值自动提取到Integer值,而使用int无法实现这一点。因此,如果我使用nullInteger会在我自己的代码中抛出,我可以修复它。