【问题标题】:javafx ComboBox<Integer>'s setValue(Integer) method causes NullpointerExceptionjavafx ComboBox<Integer> 的 setValue(Integer) 方法导致 NullpointerException
【发布时间】:2017-10-17 09:36:01
【问题描述】:

我有一个ComboBox&lt;Integer&gt;,我想在其中设置一个初始选定值。我还有一个 ChangeListener 附加到 selectedItemProperty:

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框架的bug还是我用错了?

【问题讨论】:

  • 方法playerCountChanged是什么样的?
  • 方法签名看起来像这样:“private void playerCountChanged(ObservableValue> val, int old, int newVal)”,如果这是你的问题。它适合 ChangeListener 接口
  • 您添加了一个更改侦听器,然后当您执行某些导致更改侦听器触发的操作时,您会得到一个 NPE。您应该提供更改侦听器代码,因为这可能导致您的 NPE。
  • 你不想(ObservableValue&lt;? extends Integer&gt;, Integer, Integer)吗?如果你使用'int',你可以通过强制转换获得 NPE。
  • 你应该做一个测试用例。另外,为什么不能提供代码?或者你为什么不能再做一个监听器,你可以提供代码来证明你的问题。

标签: java user-interface javafx


【解决方案1】:

错误是我的playerCountChanged(Observable&lt;?&gt;, int, int)方法的参数使用了int类型。
所以第一次选择一个值(通过调用setValue方法),没有先前选择的值,因此作为参数int old 传递的值是null。因为我使用int 而不是Integer java 尝试将Integer 值自动装箱为int 值,而null 无法做到这一点。
所以如果我改用Integer,@ 987654332@ 被抛出在我自己的代码中,我可以修复它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-27
    • 2021-12-03
    • 2019-12-01
    • 1970-01-01
    • 2020-01-26
    • 2013-04-07
    • 1970-01-01
    相关资源
    最近更新 更多