IntelliJ Idea + JavaFX = class.getResource()返回null [英] IntelliJ Idea + JavaFX = class.getResource() returns null

查看:1202
本文介绍了IntelliJ Idea + JavaFX = class.getResource()返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,这里有很多类似的问题,类似的问题,但在阅读了大量的帖子后,我不能简单地解决这个问题。
编译器>资源模式下,我已将此字符串设置为* .fxml。此外,我已将资源文件夹标记为资源根目录。

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:

这是我的 MainView.java 类。

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;
    }

}

尽管如此,我只是得到了返回null资源。任何提示?

谢谢!

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


推荐答案

过了一段时间我找到了解决这个讨厌问题的方法,我会尽力在这里解释一下。

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

首先我已经以适当的方式设置了我的项目结构(文件>项目结构......),定义了源文件夹和资源文件夹,以便IntelliJ Idea能够找到它们。此外,我已经检查过我的资源文件已经正确放入build和out文件夹(它们在构建过程中由Gradle和IntelliJ使用):我也通过设置我的文件来实现它。设置|编译器设置很好地精确定位在许多Stack Overflow答案中。

最后我在调用我的资源文件时使用了这个结构:

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 file 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"));

我想确定我的fxml文件夹的上层文件夹是Resources文件夹。

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

这篇关于IntelliJ Idea + JavaFX = class.getResource()返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆