[英]javafx ComboBox<Integer>'s setValue(Integer) method causes NullpointerException
我有一个ComboBox<Integer>
,我想在其中设置初始选定值。 我还有一个附加到 selectedItemProperty 的ChangeListener
:
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)
然而,它的代码永远不会被调用。 堆栈跟踪如下所示:
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框架中的一个错误还是我只是用错了?
错误是我对playerCountChanged(Observable<?>, int, int)
方法的参数使用了int
类型。
所以第一次选择一个值时(通过调用setValue
方法),没有先前选择的值,所以作为参数int old
传递的值是null
。 因为我使用int
而不是Integer
java 尝试将Integer
值自动装箱为int
值,这不能用null
完成。
因此,如果我改用Integer
,则会在我自己的代码中抛出NullPointerException
,我可以在其中修复它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.