簡體   English   中英

javafx 組合框<Integer>的 setValue(Integer) 方法導致 NullpointerException

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM