Исключение в ошибке метода запуска приложения. Я использую JavaFX в IntellJ - PullRequest
0 голосов
/ 09 марта 2020

Итак, когда я писал свой код, я пытался запустить свой код. Вот сообщение об ошибке:

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: 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$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)
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);
    }
}

, а вот мой код для моего файла F XML:


<?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>

Я не понимаю, что происходит, я пробовал несколько решений в Интернете, но ничего не получалось. Я попытался настроить свою виртуальную машину, чтобы добавить больше кучи, это не сработало. Я пытался добавить .module, но это дало мне странную ошибку.

1 Ответ

0 голосов
/ 09 марта 2020

Удалите тег 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, go откройте вкладку «Контроллер» и снимите флажок «Использовать fx: root construct»:

enter image description here

edit : не обращайте внимания на путь контроллера на скриншоте, просто снимите флажок, как я сказал.

...