0

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

Deviver
  • 3
  • 3
  • Can you add an [minimal, complete, and verifiable, example](https://stackoverflow.com/help/mcve)? I know with some versions of JavaFX the `ChoiceBox` returns a `null` value on the first selection. I don't know if `JFXTreeTableView` might suffer from the same. – M. le Rutte May 15 '18 at 14:22
  • @M.leRutte I've added a verifiable example – Deviver May 15 '18 at 14:37
  • calling `setShowRoot(false)` after you set the root of the table, will most likely solve your problem – Andreas May 16 '18 at 22:51
  • java naming conventions please – kleopatra Oct 12 '20 at 13:12

1 Answers1

-1

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<>();
belem
  • 341
  • 4
  • 3
  • that's not applicable here: the OP seems to want to inject the table via fxml - in that case, you __must not__ instantiate the field manually – kleopatra Oct 12 '20 at 13:10