2017-10-17 2 views
-2

私はComboBox<Integer>に初期選択値を設定します。私はまた、selectedItemPropertyに添付ChangeListenerを持っている:javafx ComboBox <Integer>のsetValue(Integer)メソッドによって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

メソッドのシグネチャは次のようになります。 "private void playerCountChanged(ObservableValue val、int old、int newVal)" ChangeListenerインターフェイスに適合します – David

+1

変更リスナーを追加すると、変更リスナーを起動させる何かを実行したときにNPEが取得されます。変更リスナーコードは、*あなたのNPEの原因となる可能性があるため、提供する必要があります。 – matt

答えて

1

私のplayerCountChanged(Observable<?>, int, int)メソッドのパラメータにはintタイプを使用していたのは間違いでした。
setValueメソッドの呼び出しによって最初に値が選択されたため、以前に選択された値がなかったので、パラメータint oldとして渡された値はnullです。私がIntegerの代わりにintを使用したため、Integerの値はの値に自動変更されます。nullでは実行できません。
代わりにIntegerを使用すると、NullPointerExceptionが自分のコードでスローされ、そこで修正できます。

関連する問題