简体   繁体   中英

JavaFX getResource not loading FXML

I'm trying to load a FXML file into a JavaFX controller with the following code:

loader.setLocation(BaseController.class.getResource("/baseLayout.fxml"));
baseLayout = loader.load();

But I'm getting this exception instead:

java.lang.IllegalStateException: Location is not set.

The problem is that getResource is not loading the FXML correctly, so setLocation receives null as parameter and I get the exception running loader.load()

What I've seen in some answers around there, is that the problem might be with wrong path in getResource. But, as far as I know, "/baseLayout.fxml" should point to the respectively fxml file inside the resources folder.

Here is my project file tree:

  • project/
    • src/
      • main/
        • java/
        • resources/
          • baseLayout.fxml

Isn't it supposed to work? I was actually working just fine, but suddenly it's not. I don't know what else to do.

You can try loading the fxml like this

loader.setLocation(BaseController.class.getResource("/resources/baseLayout.fxml"));

or

loader.setLocation(BaseController.class.getResource("../resources/baseLayout.fxml"));

But before this as @James_D mentioned Please check weather the resultant jar contains the resources folder i:e weather it is present in the build or not.

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