Sudo Sudo - 3 months ago 19
Java Question

JavaFX TreeView setRoot results in Exception

this is my first question, i feel like i'd search the whole web but anyhow there is no answer for my question. Maybe i've a simple problem - quite new to Java.
In the following Code looks all good to me. But if I execute

managertree.setRoot(rootItem)
i receive an exception.

Hope someone could spend me a minute and have a look at it.

import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.stage.Stage;

public class MainApp extends Application {

@FXML
private TreeView<String> managertree;

@Override
public void start(Stage stage) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("../view/MainLayout.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setTitle("jLog");

TreeItem<String> rootItem = new TreeItem<>("Manager");
rootItem.setExpanded(true);
for(int i = 1; i < 6;i++){
TreeItem<String> item = new TreeItem<String>("Managing Logs " + i);
rootItem.getChildren().add(item);
}
managertree.setRoot(rootItem);
managertree.setShowRoot(false);

stage.show();
}

public static void main(String[] args) {
launch(args);
}


}

The FXML:

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

<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.StackPane?>

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2">
<children>
<AnchorPane id="AnchorPane" layoutX="0.0" layoutY="0.0" prefHeight="36.0">
<children>
<MenuBar prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="Close" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit">
<items>
<MenuItem mnemonicParsing="false" text="Delete" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
</children>
</AnchorPane>
<AnchorPane prefHeight="364.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="36.0" />
<SplitPane dividerPositions="0.2826086956521739" focusTraversable="true" prefHeight="364.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="36.0">
<items>
<AnchorPane id="manager" minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<TreeView id="managertree" fx:id="managertree" editable="true" prefHeight="362.0" prefWidth="114.0" showRoot="true" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
</children>
</AnchorPane>
<AnchorPane fx:id="rawlog" minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0" />
</items>
</SplitPane>
</children>
</AnchorPane>


And the compiler throws:

Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
at jlog.main.MainApp.start(MainApp.java:32)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
... 1 more
Exception running application jlog.main.MainApp

Answer

The actual problem here might be a NullPointer exception cause fxml items(managertree [which should be written as managerTree]) have not yet been initialized.

Make your class implement Initializable interface and @override it's method.As soon as this method get called you are soore that all fxml items are not null anymore.

Then modify your fxml items there,Code:

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.stage.Stage;

public class Main extends Application implements Initializable {

    @FXML
    private TreeView<String> managertree;

    TreeItem<String> rootItem = new TreeItem<>("Manager");

    @Override
    public void start(Stage stage) throws IOException {

        //Load FXML file
        Parent root = FXMLLoader.load(getClass().getResource("MainLayout.fxml"));

        //...
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.setTitle("jLog");
        rootItem.setExpanded(true);
        for (int i = 1; i < 6; i++) {
            TreeItem<String> item = new TreeItem<String>("Managing Logs " + i);
            rootItem.getChildren().add(item);
        }

        //Show the Stage
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        System.out.println("Main Controller initialized.....");

        managertree.setRoot(rootItem);
        managertree.setShowRoot(false);

    }

}