JavaFX - 应用程序启动方法中的异常?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26451990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 02:31:49  来源:igfitidea点击:

JavaFX - Exception in Application start method?

javaexceptionjavafx

提问by re0603

I am trying to create a JavaFX program, and every time I try to run my code I am getting an exception - I'm not entirely sure what it means though...

我正在尝试创建一个 JavaFX 程序,每次我尝试运行我的代码时,我都会遇到一个异常——但我不完全确定这意味着什么......

My code:

我的代码:

package application;

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


public class Main extends Application {
@Override
   public void start(Stage stage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("DogFinderFXML.fxml"));

    Scene scene = new Scene(root);

    stage.setScene(scene);
    stage.setTitle("HelloSwingNode Sample");
    stage.show();
}

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

}

Stack trace:

堆栈跟踪:

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication7(Unknown Source)
    at com.sun.javafx.application.LauncherImpl$$Lambda/1732398722.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException: Location is required.
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.load(Unknown Source)
    at application.Main.start(Main.java:14)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication13(Unknown Source)
    at com.sun.javafx.application.LauncherImpl$$Lambda/1778973910.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait6(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$$Lambda/1051754451.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null4(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$$Lambda/813155481.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater5(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$$Lambda/1775282465.run(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null1(Unknown Source)
    at com.sun.glass.ui.win.WinApplication$$Lambda/1109371569.run(Unknown Source)
    ... 1 more
Exception running application application.Main

Any help will be appreciated,

任何帮助将不胜感激,

Thanks

谢谢

回答by DeepInJava

A simple fix: copy your fxml file/sinto the package in which your main method is currently located. Save & re-run.

一个简单的修复:将您的fxml 文件复制到您的主要方法当前所在的包中。保存并重新运行。