Search code examples
javaintellij-ideajavafxnullresources

IntelliJ Idea + JavaFX = class.getResource() returns null


I know, there are lots of similar question around here with similar problems, but after reading tons of posts, I cannot simply solve this issue. Under Compiler > Resource Patterns, I've already put this string "*.fxml". Moreover, I've marked the resources folder as the Resources Root.

Here you can see my project structure:

Project Structure

This is my MainView.java class.

package dataParser.View;

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


public class MainView extends Application {
    private Stage primaryStage;

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

    @Override
    public void start(Stage primaryStage) throws Exception {
        this.primaryStage = primaryStage;

        FXMLLoader loader = new FXMLLoader();
        System.out.println(this.getClass().getResource("/MainView.fxml"));
        loader.setLocation(getClass().getResource("/MainView.fxml"));

        MainViewController mainViewController = new MainViewController();
        mainViewController.setMainApp(this);

        loader.setController(mainViewController);
        Parent layout = loader.load();

        Scene scene = new Scene(layout);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    Stage getPrimaryStage() {
        return primaryStage;
    }

}

Still, I'm just getting back a null resource. Any hints?
Thank you!

EDIT
This is the complete stack-trace. I've also noticed that I've issues linking all my resource files.

    Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:973)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:198)
    at java.base/java.lang.Thread.run(Thread.java:844)
Caused by: java.lang.IllegalStateException: Location is not set.
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2459)
    at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2435)
    at dataParser.View.MainView.start(MainView.java:29)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:919)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(PlatformImpl.java:449)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(PlatformImpl.java:418)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:417)
    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:175)
    ... 1 more

Solution

  • After some time I found a solution to this nasty issue and I'll try my best to explain it here.

    First of all I've set up my Project Structure (File > Project Structure...) in a proper way, defining both the Source and Resource folders, so that IntelliJ Idea would be able to find them. Moreover I've checked that my resources files had been properly put in the "build" and "out" folders (they're used by Gradle and IntelliJ during the building process): I've also achieved it by setting my "File|Settings|Compiler" settings as nicely pinpointed in many Stack Overflow answers.
    At last I've used this structure while calling for my resources files:

    FXMLLoader loader = new FXMLLoader();
    System.out.println(MainView.class.getResource("/fxml/MainView.fxml"));
    loader.setLocation(MainView.class.getResource("/fxml/MainView.fxml"));
    

    I want to pinpoint that my fxml folder has the Resources one as root folder.