Java FXML Error: Exception in Application start method [duplicate]

浪尽此生 提交于 2021-01-29 19:28:52

问题


I have created a JavaFX project, and have an error upon running, I suspect the error is within the following main class:

        import java.io.IOException;
    
    import javafx.application.Application;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Scene;
    import javafx.scene.layout.AnchorPane;
    import javafx.stage.Stage;
    
    public class Main extends Application {
    
       private Stage primaryStage;
       private AnchorPane mainLayout;
    
       public void start(Stage primaryStage) throws IOException {
           this.primaryStage = primaryStage;
           this.primaryStage.setTitle("Real Estate Listings");
           showMainGUI();
       }
      
       private void showMainGUI() throws IOException {
           FXMLLoader loader = new FXMLLoader();
           loader.setLocation(Main.class.getResource("C:\\Users\\micha\\Documents\\eclipse-workspace\\ProgAssignment2\\src\\MainGUI.fxml"));
           mainLayout = loader.load();
           Scene scene = new Scene(mainLayout);
           primaryStage.setScene(scene);
           primaryStage.show();
       }
    
       public static void main(String[] args) {
           launch(args);
       }
    }

Here is the error:

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:564)
    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:564)
    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:832)
Caused by: java.lang.NullPointerException: Location is required.
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3316)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3280)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3249)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3222)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3199)
    at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3192)
    at Main.showMainGUI(Main.java:22)
    at 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)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    ... 1 more
Exception running application Main

Upon some research, I've found the error most likely resides within the calling to the FXML loader, however after trying several different fixes I have been unable to come across a solution. Am I creating the FXML object incorrectly? Thanks


回答1:


I consider to have a closer look at the NullPointerException part which is mentioned inside the stacktrace you posted. Is the path to your file correct?




回答2:


I've had a similar problem in the past. Try to change:

loader.setLocation(Main.class.getResource("C:\\Users\\micha\\Documents\\eclipse-workspace\\ProgAssignment2\\src\\MainGUI.fxml"));

to:

loader.setLocation(getClass().getClassLoader().getResource("C:\\Users\\micha\\Documents\\eclipse-workspace\\ProgAssignment2\\src\\MainGUI.fxml"));


来源:https://stackoverflow.com/questions/64835996/java-fxml-error-exception-in-application-start-method

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!