IntelliJ Idea+;JavaFX=class.getResource()返回null

IntelliJ Idea+;JavaFX=class.getResource()返回null,java,intellij-idea,javafx,null,resources,Java,Intellij Idea,Javafx,Null,Resources,我知道,这里有很多类似的问题和类似的问题,但是在阅读了大量的帖子之后,我不能简单地解决这个问题。 在编译器>资源模式下,我已经把这个字符串“*.fxml”放在了下面。此外,我还将resources文件夹标记为resources根目录 在这里您可以看到我的项目结构: 这是我的MainView.java类 package dataParser.View; import javafx.application.Application; import javafx.fxml.FXMLLoader; i

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

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

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

问题可能是IntelliJ不知道需要将.fxml文件复制到输出目录中。您可能需要将“?.fxml;?.css”之类的内容添加到文件|设置|编译器设置中,以告知它在生成过程中复制文件。有关更多信息,请参阅。

过了一段时间,我找到了这个棘手问题的解决方案,我将尽力在这里解释

首先,我以适当的方式设置了我的项目结构(文件>项目结构…),定义了源文件夹和资源文件夹,以便IntelliJ Idea能够找到它们。此外,我还检查了我的资源文件是否正确地放入了“build”和“out”文件夹(Gradle和IntelliJ在构建过程中使用了它们):我还通过将“File | Settings | Compiler”设置设置设置为许多堆栈溢出答案中精确的设置来实现这一点。
最后,我在调用我的资源文件时使用了此结构:

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

我想指出我的“fxml”文件夹的根文件夹是“Resources”文件夹。

编译成一个jar,打开它,看看MainView.fxml是否在那里。原则上(没有帮助)使用
MainView.class.getResource
而不是
getClass().getResource
。为您的项目使用maven构建基础设施,将具有现成的正确设置:src/main/resources等。我正在使用Gradle,它为我设置了一切。此外,将.fxml文件放入生成的.jar文件中。Gradle也提供了一个标准。在命令行上启动jar?我曾希望会有一个
/MainView.fxml.xml
/resources/MainView.fxml
之类的东西,但可惜的是。有一次,我的文件名中有一个
I
,而不是
I
。重新输入字符串
“/MainView.fxml”
可能有一些不可见的垃圾。因为实际上不会有太多错误。目前我只是通过IntelliJ Idea构建和运行程序。我不能简单地理解问题是什么。我试图重写字符串,并按照本文()中的提示将.fxml文件放在自定义/fxml文件夹中。这个问题快把我逼疯了。打印出这个.getClass().getResource(“./”返回的URL然后检查你的*.fxml文件是否在同一个目录中。嗨!最后,几个月前,我解决了我的问题,摆弄IntelliJ Idea项目设置,查找堆栈溢出,寻找调用文件路径的正确方法。嗨,@Davide3i。恭喜你解决了这个问题。我鼓励你用您找到的解决方案,以便任何其他有相同问题的人都可以在此处找到解决方案。(可以在搜索解决方案时,找到一个问题已得到回答的声明,但没有给出答案。)谢谢!我想补充一点,路径中的第一条斜线非常重要!