highwaycat
highwaycat

Reputation: 81

JavaFX ComboBox itemsProperty binding

I'm pretty new to JavaFX, and just tried property bindigs. It worked fine with StringProperty, and BooleanProperty, but now I have to bind some data to a ComboBox.

This is the line, where it dies. It's in a controller class for an FXML:

comboBox.itemsProperty().bind(root.itemsProperty());

Here is the class of root:

import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.ObservableList;

public class SectionComboBox extends SectionControl{

    private final ObjectProperty<ObservableList> items = new SimpleObjectProperty<>();
    }

    public ObservableList getItems() {
        return items.get();
    }

    public void setItems(ObservableList value) {
        items.set(value);
    }

    public ObjectProperty itemsProperty() {
        return items;
    }
}

The error log:

java.lang.NullPointerException
at com.sun.javafx.scene.control.skin.ComboBoxListViewSkin.getIndexOfComboBoxValueInItemsList(ComboBoxListViewSkin.java:430)
at com.sun.javafx.scene.control.skin.ComboBoxListViewSkin.updateDisplayNode(ComboBoxListViewSkin.java:376)
at com.sun.javafx.scene.control.skin.ComboBoxListViewSkin.getDisplayNode(ComboBoxListViewSkin.java:232)
at com.sun.javafx.scene.control.skin.ComboBoxBaseSkin.updateDisplayArea(ComboBoxBaseSkin.java:125)
at com.sun.javafx.scene.control.skin.ComboBoxBaseSkin.computePrefHeight(ComboBoxBaseSkin.java:180)
at javafx.scene.Parent.prefHeight(Parent.java:879)
at javafx.scene.layout.Region.prefHeight(Region.java:1384)
at javafx.scene.control.Control.computePrefHeight(Control.java:856)
at javafx.scene.Parent.prefHeight(Parent.java:879)
at javafx.scene.control.Control.prefHeight(Control.java:738)
at javafx.scene.layout.Region.computeChildPrefAreaHeight(Region.java:1599)
at javafx.scene.layout.AnchorPane.computeChildHeight(AnchorPane.java:296)
at javafx.scene.layout.AnchorPane.layoutChildren(AnchorPane.java:327)
at javafx.scene.Parent.layout(Parent.java:1018)
at javafx.scene.Parent.layout(Parent.java:1028)
at javafx.scene.Parent.layout(Parent.java:1028)
at javafx.scene.Parent.layout(Parent.java:1028)
at javafx.scene.Parent.layout(Parent.java:1028)
at javafx.scene.Scene.layoutDirtyRoots(Scene.java:516)
at javafx.scene.Scene.doLayoutPass(Scene.java:487)
at javafx.scene.Scene.access$3900(Scene.java:170)
at javafx.scene.Scene$ScenePulseListener.pulse(Scene.java:2186)
at com.sun.javafx.tk.Toolkit.firePulse(Toolkit.java:363)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:461)
at com.sun.javafx.tk.quantum.QuantumToolkit$9.run(QuantumToolkit.java:330)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:17)
at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:67)
at java.lang.Thread.run(Thread.java:724)

Can someone help me how can I bind it corretly, what did I missed?

Upvotes: 4

Views: 6454

Answers (1)

highwaycat
highwaycat

Reputation: 81

I've figured it out, since yesterday. The only problem was, that I forgot to initialize the ObservableList before binding it to the ComboBox. So, this is the missing line, fmo the very beginning:

root.setItems(FXCollections.observableArrayList());

Upvotes: 4

Related Questions