在带有 FXML 的 JavaFX 中使用 TreeTableView 的应用程序不会启动

Application with TreeTableView in JavaFX with FXML wont start

我正在尝试在 JavaFX 中构建一个简单的应用程序,它使用 TreeTableView 来显示项目,并使用几个按钮和文本字段来输入和删除这些列表中的内容。但我无法启动应用程序。我得到一个 Exception in Application start method 并且我不知道发生了什么。也许有人可以帮助我?

这是我的主class:

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

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

    @Override
    public void start (Stage primaryStage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Name");
        primaryStage.setScene(new Scene(root, 520, 620));
        primaryStage.show();
    }
}

这是我的控制器:

package sample;

import com.sun.source.tree.Tree;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeTableColumn;
import javafx.scene.control.TreeTableView;
import javafx.util.Callback;

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

public class Controller implements Initializable {

    @FXML
    private TreeTableView<String> tableView;
    @FXML
    private TreeTableColumn<String, String> col1;

        // TreeItems and Parent 1
        TreeItem<String> item1_l1 = new TreeItem<>("Name_Item1_Level1");
        TreeItem<String> item2_l1 = new TreeItem<>("Name_Item2_Level1");
        TreeItem<String> item3_l1 = new TreeItem<>("Name_Item3_Level1");
        TreeItem<String> parent1 = new TreeItem<>("Parent1");

        TreeItem<String> item1_l2 = new TreeItem<>("Name_Item1_Level2");
        TreeItem<String> item2_l2 = new TreeItem<>("Name_Item2_Level2");
        TreeItem<String> item3_l2 = new TreeItem<>("Name_Item3_Level2");
        TreeItem<String> parent2 = new TreeItem<>("parent2");

        // Setting up the Root for TreeItem
        TreeItem<String> root = new TreeItem<>("Root");

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        parent1.getChildren().setAll(item1_l1, item2_l1, item3_l1);
        parent2.getChildren().setAll(item1_l2, item2_l2, item3_l2);

        root.getChildren().setAll(parent1, parent2);
        tableView.setRoot(root);

        col1.setCellValueFactory(
                (TreeTableColumn.CellDataFeatures<String, String>param) -> new SimpleStringProperty(param.getValue().getValue()));
    }
}

这是我的 sample.fxml 文件:

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

<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="620.0" prefWidth="520.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
   <center>
      <VBox BorderPane.alignment="CENTER">
         <children>
            <MenuBar>
              <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>
            <TreeTableView prefHeight="556.0" prefWidth="520.0">
              <columns>
                <TreeTableColumn prefWidth="270.0" text="C1" />
              </columns>
            </TreeTableView>
            <HBox spacing="20.0" VBox.vgrow="ALWAYS">
               <children>
                  <TextField />
                  <TextField />
                  <Region prefHeight="27.0" prefWidth="47.0" HBox.hgrow="ALWAYS" />
                  <Button mnemonicParsing="false" text="Add" />
               </children>
               <VBox.margin>
                  <Insets bottom="8.0" left="8.0" right="8.0" top="8.0" />
               </VBox.margin>
            </HBox>
         </children>
      </VBox>
   </center>
</BorderPane>

最后但同样重要的是,这是我在尝试启动应用程序时遇到的错误:

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication(LauncherImpl.java:195)
    at java.base/java.lang.Thread.run(Thread.java:830)
Caused by: javafx.fxml.LoadException: 
/Users/c-mon/IdeaProjects/Guccifer%20copy/out/production/Guccifer/sample/sample.fxml

    at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2625)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2603)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3237)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3194)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3163)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3136)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3113)
    at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3106)
    at sample.Main.start(Main.java:17)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1(LauncherImpl.java:846)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait(PlatformImpl.java:455)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
Caused by: java.lang.NullPointerException
    at sample.Controller.initialize(Controller.java:43)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2573)
    ... 14 more
Exception running application sample.Main

您的 tableViewcol1 未绑定到布局标签,因此 NPE 发生在 tableView.setRoot() 中。

您应该对这两个元素执行此操作:<TreeTableView prefHeight="556.0" prefWidth="520.0" fx:id="tableView"> 然后从 FXML 创建的 TreeTableView 将写入控制器的 tableView 字段。