应用程序启动方法错误中的异常。我在 IntelliJ 中使用 JavaFX

Exception in application start method error. I am using JavaFX in IntellJ

所以当我写我的代码时,我试图 运行 我的代码。 这是错误消息:

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: Root hasn't been set. Use method setRoot() before load.
/Users/anish/IdeaProjects/FBLA2020CompetitionCode/out/production/FBLA2020CompetitionCode/sample/HOME.fxml:17

    at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2621)
    at javafx.fxml/javafx.fxml.FXMLLoader$RootElement.constructValue(FXMLLoader.java:1338)
    at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:754)
    at javafx.fxml/javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2722)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2552)
    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:13)
    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)
Exception running application sample.Main

这是我的代码 Main.java:

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 {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("HOME.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 380, 275));
        primaryStage.show();
    }


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

这是我的 FXML 文件代码:


<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.Region?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>

<fx:root maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="540.0" prefWidth="1000.0" style="-fx-background-color: #081B33;" type="AnchorPane" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
    <children>
        <VBox prefHeight="540.0" prefWidth="260.0" style="-fx-background-color: #2F4562;" AnchorPane.bottomAnchor="0.0" AnchorPane.topAnchor="0.0">
            <children>
                <ImageView fitHeight="179.0" fitWidth="266.0" pickOnBounds="true" preserveRatio="true">
                    <Image url="@../images/Flag-color.png" />
                </ImageView>
                <Button fx:id="homeButton" alignment="BASELINE_LEFT" graphicTextGap="20.0" mnemonicParsing="false" prefHeight="70.0" prefWidth="260.0" style="-fx-background-color: #2f4562;" text="The Hub" textFill="WHITE">
                    <graphic>
                        <ImageView fitHeight="23.0" fitWidth="27.0" pickOnBounds="true" preserveRatio="true">
                            <image>
                                <Image url="@../images/icons8_Home_32px.png" />
                            </image>
                        </ImageView>
                    </graphic>
                    <padding>
                        <Insets left="50.0" />
                    </padding>
                </Button>
                <Button fx:id="studentButton" alignment="BASELINE_LEFT" graphicTextGap="20.0" mnemonicParsing="false" onAction="#HandleButtonAction" prefHeight="70.0" prefWidth="260.0" style="-fx-background-color: #2F4562;" text="Student" textFill="WHITE">
                    <graphic>
                        <ImageView fitHeight="23.0" fitWidth="27.0" pickOnBounds="true" preserveRatio="true">
                            <image>
                                <Image url="@../images/icons8_Person_32px.png" />
                            </image>
                        </ImageView>
                    </graphic>
                    <padding>
                        <Insets left="50.0" />
                    </padding>
                </Button>
                <Button fx:id="editButton" alignment="BASELINE_LEFT" graphicTextGap="20.0" mnemonicParsing="false" prefHeight="70.0" prefWidth="260.0" style="-fx-background-color: #2f4562;" text="Edit" textFill="WHITE">
                    <graphic>
                        <ImageView fitHeight="23.0" fitWidth="27.0" pickOnBounds="true" preserveRatio="true">
                            <image>
                                <Image url="@../images/icons8_Slider__32px.png" />
                            </image>
                        </ImageView>
                    </graphic>
                    <padding>
                        <Insets left="50.0" />
                    </padding>
                </Button>
                <Button fx:id="printButton" alignment="BASELINE_LEFT" graphicTextGap="20.0" mnemonicParsing="false" prefHeight="70.0" prefWidth="260.0" style="-fx-background-color: #2F4562;" text="Print" textFill="WHITE">
                    <graphic>
                        <ImageView fitHeight="23.0" fitWidth="27.0" pickOnBounds="true" preserveRatio="true">
                            <image>
                                <Image url="@../images/icons8_Open_Envelope_32px.png" />
                            </image>
                        </ImageView>
                    </graphic>
                    <padding>
                        <Insets left="50.0" />
                    </padding>
                </Button>
            </children>
        </VBox>
        <Label layoutX="292.0" layoutY="27.0" text="FBLA Community Service" textFill="WHITE">
            <font>
                <Font name="System Font" size="24.0" />
            </font>
        </Label>
        <HBox alignment="CENTER" layoutX="292.0" layoutY="75.0" prefHeight="92.0" prefWidth="650.0" spacing="50.0">
            <children>
                <VBox alignment="CENTER" prefHeight="92.0" prefWidth="125.0">
                    <children>
                        <Label alignment="CENTER" prefHeight="31.0" prefWidth="90.0" text="N/A" textFill="#de0000">
                            <font>
                                <Font name="System Bold" size="26.0" />
                            </font>
                        </Label>
                        <Label alignment="CENTER" prefHeight="17.0" prefWidth="88.0" text="Total Hours" textFill="WHITE" />
                    </children>
                </VBox>
                <Label />
                <Label />
                <VBox alignment="CENTER" prefHeight="92.0" prefWidth="125.0">
                    <children>
                        <Label alignment="CENTER" prefHeight="17.0" prefWidth="88.0" text="N/A" textFill="#de0000">
                            <font>
                                <Font size="26.0" />
                            </font>
                        </Label>
                        <Label prefHeight="17.0" prefWidth="129.0" text="Most Hours Worked" textFill="WHITE" />
                    </children>
                </VBox>
            </children>
            <padding>
                <Insets left="5.0" />
            </padding>
        </HBox>
        <HBox alignment="CENTER_LEFT" layoutX="292.0" layoutY="174.0" style="-fx-background-color: #2F4562;">
            <children>
                <Label alignment="CENTER" prefHeight="43.0" prefWidth="76.0" text="Search:" textFill="WHITE">
                    <padding>
                        <Insets left="10.0" right="10.0" />
                    </padding>
               <font>
                  <Font name="System Bold Italic" size="13.0" />
               </font>
                </Label>
                <TextField alignment="CENTER" prefHeight="40.0" prefWidth="157.0" promptText="search students">
                    <padding>
                        <Insets left="10.0" right="10.0" />
                    </padding>
                </TextField>
            <Region prefHeight="43.0" prefWidth="250.0" />
                <Button fx:id="print1Button" alignment="CENTER" mnemonicParsing="false" prefHeight="32.0" prefWidth="164.0" style="-fx-background-color: #2F4562 #2F4562;" text="Print" textFill="WHITE">
                    <font>
                        <Font size="16.0" />
                    </font></Button>
            </children></HBox>
        <VBox fx:id="pnitems" layoutX="292.0" layoutY="227.0" prefHeight="300.0" prefWidth="650.0">
            <children>
                <TableView style="-fx-background-color: #081b33;">
                    <columns>
                        <TableColumn minWidth="-1.0" prefWidth="75.0" text="Student" />
                        <TableColumn minWidth="-1.0" prefWidth="75.0" text="ID" />
                        <TableColumn minWidth="-1.0" prefWidth="75.0" text="Grade" />
                        <TableColumn minWidth="-1.0" prefWidth="75.0" text="Hours" />
                        <TableColumn minWidth="-1.0" prefWidth="75.0" text="Awards" />
                    </columns>
                    <columnResizePolicy>
                        <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
                    </columnResizePolicy>
                </TableView>
            </children></VBox>
        <Label layoutX="292.0" layoutY="6.0" prefHeight="18.0" prefWidth="148.0" text="Error: N/A" textFill="#e80909" />
    </children>
</fx:root>

我对发生的事情感到困惑,我在网上尝试了多种解决方案,但没有任何效果。我尝试配置我的 VM 以添加更多堆,但没有用。我试图添加一个 .module,但这给了我一个奇怪的错误。

删除标签 fx:root 并将其替换为 AnchorPane :

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="540.0" prefWidth="1000.0" style="-fx-background-color: #081B33;" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
    <children>
        <VBox prefHeight="540.0" prefWidth="260.0" style="-fx-background-color: #2F4562;" AnchorPane.bottomAnchor="0.0" AnchorPane.topAnchor="0.0">
            <children>
                ...

或者,如果您使用 SceneBuilder,请转到“控制器”选项卡并取消选中 "Use fx:root construct":

编辑 : 不要介意屏幕截图中的控制器路径,只需按照我说的取消选中该框即可。