简体   繁体   中英

TreeItem value is null, throwing Null Pointer Exception

I am using JFoenix library to build my UI.

I have a JFXTreeTableView mapped through FXML in my controller class:

@FXML private JFXTreeTableView<MyObject> table;

The data I use to fill in the columns is stored in ObservableList which does not contain any null data as debugger shows:

ObservableList<MyObject> list = FXCollections.observableArrayList(task.getValue());

After that I create a new JFXTreeTableColumn :

JFXTreeTableColumn<MyObject, String> Column1 = new JFXTreeTableColumn<>("Column1");

Now the Null Pointer Exception occurs when I try to setCellValueFactory() on the column like so:

nameColumn.setCellValueFactory(param -> new SimpleStringProperty(param.getValue().getValue().getStringValue());

Debugger shows that param TreeItem value is null. But I don't undestand why it is null. I have almost the same code in another controller, which works perfectly.

Verifiable example:

FXML

<?xml version="1.0" encoding="UTF-8"?>

<?import com.jfoenix.controls.JFXButton?>
<?import com.jfoenix.controls.JFXSpinner?>
<?import com.jfoenix.controls.JFXTabPane?>
<?import com.jfoenix.controls.JFXTreeTableView?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TreeTableView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.StackPane?>

<StackPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity"
       prefHeight="768.0" prefWidth="1366.0"
       xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1"
       fx:controller="sample.Controller">
<JFXTabPane>
    <Tab text="Home">
        <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
            <JFXTreeTableView fx:id="table" layoutY="86.0" prefHeight="636.0" prefWidth="1366.0"
                              AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0"
                              AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="86.0">
                <columnResizePolicy>
                    <TreeTableView fx:constant="CONSTRAINED_RESIZE_POLICY"/>
                </columnResizePolicy>
            </JFXTreeTableView>
            <JFXButton layoutX="180.0" layoutY="29.0" onAction="#ExampleMethod" prefHeight="31.0"
                       prefWidth="83.0" text="Refresh"/>
        </AnchorPane>
    </Tab>
</JFXTabPane>
</StackPane>

Controller class

public class Controller {

@FXML
private JFXTreeTableView<MyObject> table;

@FXML
public void ExampleMethod() {
    ObservableList<MyObject> list = FXCollections.observableArrayList();

    list.add(new MyObject("test_value"));

    JFXTreeTableColumn<MyObject, String> column = new JFXTreeTableColumn<>("Name");

    column.setCellValueFactory(param -> new SimpleStringProperty(param.getValue().getValue().getSomeValue()));

    final TreeItem<MyObject> root = new RecursiveTreeItem<>(list, RecursiveTreeObject::getChildren);

    table.getColumns().setAll(column);

    table.setRoot(root);
}
}

MyObject

package sample;

import com.jfoenix.controls.datamodels.treetable.RecursiveTreeObject;

public class MyObject extends RecursiveTreeObject<MyObject> {
private final String SomeValue;

public MyObject(String someValue) {
    SomeValue = someValue;
}

public String getSomeValue() {
    return SomeValue;
}

}

Appreciate any help,

Thank you

i also got this error and solved this by declare my variable. In your case,

private JFXTreeTableView<MyObject> table;

to

private JFXTreeTableView<MyObject> table = new JFXTreeTableView<>();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM