Приложение с TreeTableView в JavaFX с F XML не запустится - PullRequest
0 голосов
/ 21 февраля 2020

Я пытаюсь создать простое приложение в JavaFX, которое использует TreeTableView для отображения элементов и пару кнопок и текстовых полей для ввода и удаления содержимого этих списков. Но я не могу получить приложение для запуска. Я получаю Exception in Application start method, и я понятия не имею, что происходит. Может быть, кто-то может мне помочь?

Это мой основной класс:

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

А это мой образец. * file:

<?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$2(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$9(LauncherImpl.java:846)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(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

1 Ответ

1 голос
/ 21 февраля 2020

Ваши tableView и col1 не привязаны к тегам макета, поэтому NPE происходит в tableView.setRoot().

Вы должны сделать это для обоих элементов: <TreeTableView prefHeight="556.0" prefWidth="520.0" fx:id="tableView">, тогда TreeTableView, созданное из F XML, будет записано в tableView поле контроллера.

...