0

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

Memzi
  • 27
  • 6
  • Try to completely `clean and build` your project. IDE's cache is known to interfere with `FXMLLoader`'s reflective nature. This sometimes happens to me too. – Fureeish Nov 14 '20 at 16:50
  • @Fureeish I have tried this, and I'm still greeted with the same error. Would providing more classes help with this? I'm new to JavaFX and based on what I've seen the issue stems from the main class, but is it possible other ones are to blame too? I only see "main" listed in the error. – Memzi Nov 14 '20 at 16:53
  • `Location is required.`, in my experience, can mean a lot of different things. Notably, what I see is different in your example than in the typical one, I see that you are using an absolute path. Try adding `/` before the path. Do you have a `resources` folder? Judging from your path, you do not. It is a good practice (for situations exactly like this one) to have one. Are you using some kind of tutorial for FX? Is this their exact code? – Fureeish Nov 14 '20 at 17:08

2 Answers2

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

Stibb84
  • 106
  • 5
  • The path to the file is correct, interestingly enough I have a different error on the same line when running now. It is Java.Lang.IllegalStateException – Memzi Nov 14 '20 at 16:50
-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"));