私は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フレームワークのバグですか、私は間違って使用しましたか?
「playerCountChanged」メソッドはどのように見えますか? – matt
メソッドのシグネチャは次のようになります。 "private void playerCountChanged(ObservableValue > val、int old、int newVal)" ChangeListenerインターフェイスに適合します – David
変更リスナーを追加すると、変更リスナーを起動させる何かを実行したときにNPEが取得されます。変更リスナーコードは、*あなたのNPEの原因となる可能性があるため、提供する必要があります。 – matt