TreeItem值为null,抛出Null Pointer Exception

时间:2018-05-15 14:19:46

标签: java javafx jfoenix

我正在使用JFoenix库来构建我的UI。

我在控制器类中通过FXML映射JFXTreeTableView

@FXML private JFXTreeTableView<MyObject> table;

我用来填充列的数据存储在ObservableList中,调试器显示不包含任何空数据:

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

之后我创建了一个新的JFXTreeTableColumn

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

现在当我在列上尝试setCellValueFactory()时出现空指针异常:

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

调试器显示param TreeItem值为空。但我不明白为什么它是空的。我在另一个控制器中有几乎相同的代码,它完美地运行。

可验证的例子:

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>

控制器类

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;
}

}

感谢任何帮助,

谢谢

1 个答案:

答案 0 :(得分:-1)

我也遇到了这个错误,并通过声明我的变量解决了这个问题。 就您而言,

private JFXTreeTableView<MyObject> table;

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