【问题标题】:How to fix this "Exception in Application start method"? [duplicate]如何解决此“应用程序启动方法中的异常”? [复制]
【发布时间】:2020-06-05 05:08:30
【问题描述】:

我是 JavaFX 新手,我试图使用 Netbeans IDE 制作 JavaFX FXML 应用程序项目,但我遇到了这个异常。

这是我的 java 类:

    public class JavaFXApplication9 extends Application {

        @Override
        public void start(Stage stage) throws Exception {
            Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));

            Scene scene = new Scene(root);

            stage.setScene(scene);
            stage.show();
        }

        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            launch(args);
        }

    }

这是我的 FXML 代码:

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

    <?import javafx.scene.control.Button?>
    <?import javafx.scene.layout.AnchorPane?>

    <AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.171" fx:controller="javafxapplication9.FXMLDocumentController">
        <children>
            <Button fx:id="button" layoutX="126" layoutY="90" onAction="#handleButtonAction" text="Click Me!" />
        </children>
    </AnchorPane>

这是我的控制器类:

    public class FXMLDocumentController implements Initializable {

        @FXML
        private Label label;

        @FXML
        private void handleButtonAction(ActionEvent event) {
            System.out.println("You clicked me!");
            label.setText("Hello World!");
        }

        @Override
        public void initialize(URL url, ResourceBundle rb) {
            // TODO
        }    

    }

这是应用程序启动方法java.lang.reflect.InvocationTargetException javafx中的异常

java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    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(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
    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(Thread.java:745)
    Caused by: java.lang.NullPointerException: Location is required.
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3207)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at javafxapplication9.JavaFXApplication9.start(JavaFXApplication9.java:22)
    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 javafxapplication9.JavaFXApplication9
    E:\Netbeans Project\JavaFXApplication9\nbproject\build-impl.xml:1052: The following error occurred 
    while executing this line:
    E:\Netbeans Project\JavaFXApplication9\nbproject\build-impl.xml:806: Java returned: 1
    BUILD FAILED (total time: 1 second)

【问题讨论】:

  • 您发布的代码对我有用。类 FXMLDocumentController 是否在包 javafxapplication9 中?文件 FXMLDocument.fxml 是否与文件 JavaFXApplication9.class 位于同一目录中?注意我问的是编译的类和不是源代码文件。
  • 如果您的项目结构没问题,您可能还需要检查answers to this question
  • 堆栈跟踪显示此位置:Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));

标签: java javafx netbeans


【解决方案1】:

此异常的主要原因是 NullPointerException(请参阅堆栈跟踪:Location is required)。因此,您尝试使用未启动的东西。 没有任何代码 sn-ps 很难找到错误。

【讨论】:

  • 堆栈跟踪显示此位置:Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
  • @MirwaisFaizi 确保您的 FXMLDocument.fxml 与 JavaFXApplication9 类位于同一文件夹中。对于所有意图和目的,getClass() 方法获取调用它的类的位置,然后 getResource() 方法找到相关的资源。所以 getClass().getResource("FXMLDocument.fxml") 指向同一个文件夹中的一个文件。
  • 我的Java类、控制器类和FXML类在同一个文件夹中。
猜你喜欢
  • 2014-12-14
  • 2021-11-29
  • 1970-01-01
  • 1970-01-01
  • 2020-06-16
  • 1970-01-01
  • 2016-05-18
  • 2018-05-29
相关资源
最近更新 更多