2017-10-17 40 views
-2

我有一个ComboBox<Integer>其中欲设定的初始设定值给。我还具有连接到一个selectedItemProperty ChangeListenerJavaFX的组合框<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框架中的错误还是我只是使用它错了?

+0

什么方法'playerCountChanged'是什么样子? – matt

+0

方法签名看起来像:“私人无效playerCountChanged(ObservableValue VAL,诠释老,诠释的newval)”,如果这是你的问题。它适合ChangeListener接口 – David

+1

您添加一个更改侦听器,然后当您执行导致更改侦听器触发的任务时获得NPE。您应该提供更改侦听器代码,因为*可能会导致您的NPE。 – matt

回答

1

错误在于我使用int类型作为我的playerCountChanged(Observable<?>, int, int)方法的参数。
因此,第一次选择一个值(通过调用setValue方法)时,没有先前选定的值,因此作为参数int old传递的值为null。因为我使用int而不是Integer,java会尝试将Integer值自动装箱到int值,这不能用null完成。
因此,如果我使用Integer代替,则NullPointerException将引发到我自己的代码中,我可以修复它。

相关问题