FXML file is not being reached

60 Views Asked by At

I want to load a GUI using a fxml file. However after multiple attempts into fixing it, it keeps returning that the file is not found and/or that it is null. I have tried to place the file within my Graphics package but that does not work. Here is my path from my project: src/main/java/com/example/javapoker/Graphics/GUI.fxml.

My code:

package com.example.javapoker.Graphics;

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

import java.io.IOException;
import java.net.URL;

public class Main extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws IOException {
        // Load the FXML file using getResource() from the class loader
        URL url = getClass().getResource("/com/example/javapoker/Graphics/GUI.fxml");
        String classpath = System.getProperty("java.class.path");
        System.out.println("Classpath: " + classpath);

        if (url != null) {
            FXMLLoader loader = new FXMLLoader(url);
            primaryStage.setScene(new Scene(loader.load(), 400, 400));
            primaryStage.setTitle("Hello JavaFX");
            primaryStage.show();
        } else {
            System.err.println("Resource 'GUI.fxml' not found");
        }
    }

}

Here is my stack trace error:

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:119)
    at java.base/java.lang.reflect.Method.invoke(Method.java:578)
    at [email protected]/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
    at [email protected]/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
    at java.base/java.lang.reflect.Method.invoke(Method.java:578)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1081)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at [email protected]/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:893)
    at [email protected]/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
    at java.base/java.lang.Thread.run(Thread.java:1623)
Caused by: java.lang.IllegalStateException: Location is not set.
    at [email protected]/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2556)
    at [email protected]/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2531)
    at com.example.javapoker/com.example.javapoker.Graphics.Main.start(Main.java:25)
    at [email protected]/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:839)
    at [email protected]/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:483)
    at [email protected]/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:456)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:400)
    at [email protected]/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:455)
    at [email protected]/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at [email protected]/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at [email protected]/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:185)
    ... 1 more
Exception running application com.example.javapoker.Graphics.Main

Note: ignore the null conditions, I deleted the condition so it could give me the stack trace erors.

0

There are 0 best solutions below