简体   繁体   中英

Can't load fxml file by FXMLLoader (InvocationTargetException)

I first time try to use JavaFx with Maven. By this topic: link IntelliJ can't recognize JavaFX 11 with OpenJDK 11 , I configured project. But whatever I do I can't load fxml file, because "getClass().getResource(path)" return 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

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 . In this situation relative path you use resolves to non-existent com.gihub.PavelKisliuk/fxml/Input.fxml .

Solutions:

  1. Preferrable: move resources into 'com.gihub.PavelKisliuk'
  2. Move Main class out of 'com.gihub.PavelKisliuk'

Hope this helps...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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