【问题标题】:IntelliJ Idea + JavaFX = class.getResource() returns nullIntelliJ Idea + JavaFX = class.getResource() 返回 null
【发布时间】:2018-06-05 00:18:59
【问题描述】:

我知道,这里有很多类似的问题有类似的问题,但是在阅读了大量的帖子之后,我不能简单地解决这个问题。 在 Compiler > Resource Patterns 下,我已经输入了这个字符串“*.fxml”。此外,我已将资源文件夹标记为资源根目录。

在这里你可以看到我的项目结构:

这是我的 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;
    }

}

不过,我只是取回了一个空资源。有什么提示吗?
谢谢!

编辑
这是完整的堆栈跟踪。我还注意到我在链接所有资源文件时遇到问题。

    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

【问题讨论】:

  • 编译成一个jar,打开,看看有没有MainView.fxml。原则上(不会有帮助)使用MainView.class.getResource 而不是getClass().getResource。为您的项目使用 maven 构建基础架构,将具有开箱即用的正确设置:src/main/resources 等。
  • 嗨。我正在使用 Gradle,它为我设置了一切。此外,.fxml 文件在生成的 .jar 文件中。
  • Gradle 也提供了一个标准。在命令行上启动 jar?我曾希望会有一个/MainView.fxml.xml/resources/MainView.fxml 或类似的,但唉。有一次我在文件名中有一个í 而不是i。重新输入字符串"/MainView.fxml" 可能有一些看不见的垃圾。因为实际上不会有太大的错误。
  • 目前我只是通过 IntelliJ Idea 构建和运行程序。我不能简单地理解问题可能是什么。我尝试重写字符串并遵循这篇文章 (stackoverflow.com/questions/20507591/…) 中的提示,他们说将 .fxml 文件放在自定义 /fxml 文件夹中。这个问题让我抓狂。
  • 打印出 this.getClass().getResource("./) 返回的 URL 然后检查你的 *.fxml 文件是否在同一个目录中。

标签: java intellij-idea javafx null resources


【解决方案1】:

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

首先,我以正确的方式设置了我的项目结构(文件 > 项目结构...),定义了 Source 和 Resource 文件夹,以便 IntelliJ Idea 能够找到它们。此外,我检查了我的资源文件是否已正确放入“build”和“out”文件夹中(Gradle 和 IntelliJ 在构建过程中使用它们):我还通过设置我的“File|”来实现它。设置|编译器”设置在许多 Stack Overflow 答案中都得到了很好的定位。
最后我在调用我的资源文件时使用了这个结构:

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

我想确定我的“fxml”文件夹的根文件夹是“资源”文件夹。

【讨论】:

  • 谢谢!我要补充一点,路径中的第一个斜杠非常重要!
【解决方案2】:

问题可能是 IntelliJ 不知道需要将 .fxml 文件复制到输出目录中。您可能需要在文件|设置|编译器设置中添加类似“;?.fxml;?.css”的内容,以告诉它在构建期间复制文件。请参阅How to convert a normal java project in intellij into a JavaFx project 了解更多信息。

【讨论】:

  • 嗨!最后,几个月前,我解决了摆弄 IntelliJ Idea 项目设置的问题,并在 Stack Overflow 上寻找调用文件路径的正确方法。
  • 嗨,@Davide3i。恭喜您解决了问题。我鼓励您使用找到的解决方案更新此错误,以便遇到相同问题的其他人可以在这里找到解决方案。 (可以是Very Frustrating,在搜索解决方案以找到已回答问题但未给出答案的声明时。)
猜你喜欢
  • 2013-03-12
  • 2012-01-13
  • 2016-03-08
  • 2014-12-07
  • 2020-03-27
相关资源
最近更新 更多