我有一個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