简体   繁体   English

无法通过FXMLLoader加载fxml文件(InvocationTargetException)

[英]Can't load fxml file by FXMLLoader (InvocationTargetException)

I first time try to use JavaFx with Maven. 我第一次尝试将JavaFx与Maven结合使用。 By this topic: link IntelliJ can't recognize JavaFX 11 with OpenJDK 11 , I configured project. 通过本主题:链接IntelliJ无法识别带有我配置的项目的OpenJDK 11的JavaFX 11 But whatever I do I can't load fxml file, because "getClass().getResource(path)" return null. 但是无论如何我都无法加载fxml文件,因为“ getClass()。getResource(path)”返回null。

I changed path, start it with '/' and without, changed packages, created packages, deleted packages, changed references in module-info, but this is not work. 我更改了路径,以“ /”开头,并且没有,更改了软件包,创建了软件包,删除了软件包,更改了模块信息中的引用,但这是行不通的。

struct: https://ibb.co/Hhwzk8b 结构: https//ibb.co/Hhwzk8b

module LogAggregator {
    requires javafx.fxml;
    requires javafx.controls;

    opens fxml to javafx.fxml;
    exports com.github.PavelKisliuk;
}

//---------------------------------------------------- // ------------------------------------------------ ----

public class Main extends Application {
    public void start(Stage primaryStage) throws Exception {

              String path = "fxml/Input.fxml";
              FXMLLoader fxmlLoader = new FXMLLoader();
      fxmlLoader.setLocation(getClass().getResource(path));
      Parent fxmlMainWindow = fxmlLoader.load();

      //start-up window
      //-----------------------------------------------
      Scene s = new Scene(fxmlMainWindow);
      primaryStage.setScene(s);
      primaryStage.show();
  }

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

May be somebody know this problem and can help me. 可能有人知道这个问题,可以为我提供帮助。 Without maven I don't have any problem's. 没有行家,我没有任何问题。

DECISION 决定

Such path: 这样的路径:

String path = "/fxml/Input.fxml";

Plus two string's to module-info: 在模块信息中加上两个字符串:

opens com.github.PavelKisliuk.controller to javafx.fxml;
exports com.github.PavelKisliuk.controller to javafx.fxml;

Your Main class seems to be in the package com.gihub.PavelKisliuk , but resources are under fxml . 您的Main类似乎在com.gihub.PavelKisliuk包中,但资源位于fxml下。 In this situation relative path you use resolves to non-existent com.gihub.PavelKisliuk/fxml/Input.fxml . 在这种情况下,您使用的相对路径解析为不存在的com.gihub.PavelKisliuk/fxml/Input.fxml

Solutions: 解决方案:

  1. Preferrable: move resources into 'com.gihub.PavelKisliuk' 首选:将资源移至“ com.gihub.PavelKisliuk”
  2. Move Main class out of 'com.gihub.PavelKisliuk' 将主类移出com.gihub.PavelKisliuk

Hope this helps... 希望这可以帮助...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM