我有一个ComboBox<Integer>
其中欲设定的初始设定值给。我还具有连接到一个selectedItemProperty ChangeListener
:JavaFX的组合框<Integer>的的setValue(整数)方法导致的NullPointerException
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'是什么样子? – matt
方法签名看起来像:“私人无效playerCountChanged(ObservableValue > VAL,诠释老,诠释的newval)”,如果这是你的问题。它适合ChangeListener接口 – David
您添加一个更改侦听器,然后当您执行导致更改侦听器触发的任务时获得NPE。您应该提供更改侦听器代码,因为*可能会导致您的NPE。 – matt