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

倖福魔咒の 提交于 2019-12-14 03:56:22

问题


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:

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

回答1:


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 as root folder the "Resources" one.




回答2:


The problem may be that IntelliJ does not know that it needs to copy the .fxml file into the output directory. You may need to add something like ";?.fxml;?.css" to your File|Settings|Compiler settings to tell it to copy the files during a build. See How to convert a normal java project in intellij into a JavaFx project for more information.



来源:https://stackoverflow.com/questions/47939908/intellij-idea-javafx-class-getresource-returns-null

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