简体   繁体   English

javafx 组合框<Integer>的 setValue(Integer) 方法导致 NullpointerException

[英]javafx ComboBox<Integer>'s setValue(Integer) method causes NullpointerException

I have a ComboBox<Integer> where I want to set an initial selected value to.我有一个ComboBox<Integer> ,我想在其中设置初始选定值。 I also have a ChangeListener attached to the selectedItemProperty:我还有一个附加到 selectedItemProperty 的ChangeListener

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

cbPlayerCount.setValue(2);

The call of the setValue method triggers a chain of propertyChangeListeners (mine not included) and finally throws a NullpointerException . setValue方法的调用会触发一系列 propertyChangeListeners(我的不包括在内)并最终抛出NullpointerException The signature of my listener method looks like this:我的侦听器方法的签名如下所示:

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

However the code of it is never invoked.然而,它的代码永远不会被调用。 The stacktrace looks like this:堆栈跟踪如下所示:

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)

I am using a jdk1.8.0_92.我正在使用 jdk1.8.0_92。 There is no error message or anything, just the Exception.没有错误消息或任何东西,只有异常。 I tried commenting the code that adds the ChangeListener, even though that code is never invoked.我尝试评论添加 ChangeListener 的代码,即使该代码从未被调用。 Without the listener attached, the Exception doesn't appear.如果没有附加侦听器,则不会出现异常。 But I still don't know why it is thrown when the listener is added.但是我仍然不知道为什么添加侦听器时会抛出它。 I don't want to debug the framework code to look for the mistake that leads to this issue.我不想调试框架代码来查找导致此问题的错误。 Why is this Exception thrown?为什么会抛出这个异常? Is it a bug in the javafx framework or did I just use it wrong?这是javafx框架中的一个错误还是我只是用错了?

The mistake was that I used the int type for the parameters of my playerCountChanged(Observable<?>, int, int) method.错误是我对playerCountChanged(Observable<?>, int, int)方法的参数使用了int类型。
So the first time a value is selected (by the call of the setValue method), there was no previous selected value, so the value passed as the parameter int old is null .所以第一次选择一个值时(通过调用setValue方法),没有先前选择的值,所以作为参数int old传递的值是null Because I used int instead of Integer java tries to autobox the Integer value into an int value, which cannot be done with null .因为我使用int而不是Integer java 尝试将Integer值自动装箱为int值,这不能用null完成。
So if I use Integer instead, the NullPointerException is thrown in my own code where I can fix it.因此,如果我改用Integer ,则会在我自己的代码中抛出NullPointerException ,我可以在其中修复它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM