0

I face a problem with a project. I make a simple game in Eclipse using JavaFX. In the beginning I faced a problem with this command in FXMLLoader

root = FXMLLoader.load(Main.class.getResource("\\application\\design.fxml")); .

When I changed the command to

root = FXMLLoader.load(getClass().getClassLoader().getResource("\\application\\design.fxml"));

the program runs perfect. Now I decided to export the project to a runnable jar but when I try to run the jar it's not working. I used exe4jwizard and I found out that the problem was on FXMLLoader with this error:

java.lang.NullPointerException: Location is required.

Nellie Danielyan
  • 1,001
  • 7
  • 19
  • 1
    please read the api doc of getResource, both in class and classLoader ... a) they differ in how the path is resolved b) nowhere does it state to support backslashes .. – kleopatra Apr 26 '19 at 11:29
  • Thanks for your answer! I understand that they differ in how the path is resolved but how is possible to find the path when I run it in Eclipse and not in Jar? – Foivos Polyzos Apr 26 '19 at 13:44
  • don't know (and it's not relevant) - stick to the specification and be happy always :) – kleopatra Apr 26 '19 at 14:22
  • Haha thanks but my kid still not running! :( – Foivos Polyzos Apr 26 '19 at 14:23
  • check if the fxml is where you think it is, check whether the spelling is correct, check lower/upper case, check the package ... more often than not it's a minor oversight that causes such problems – kleopatra Apr 26 '19 at 14:26
  • And if all that is okay, check to make sure your FXML files are actually included in the JAR file. – Slaw Apr 27 '19 at 01:49
  • how can I check this... I mean maybe that's the problem so how can I add the fxml file in the jar? – Foivos Polyzos Apr 27 '19 at 12:15
  • A JAR file is, at least fundamentally, just a ZIP file—you can inspect its contents similarly. Or use the `jar` tool to list the contents. If your FXML files are not being included, read the documentation of whatever IDE and/or build tool you're using to figure out how to include resources when building the JAR file. If they _are_ being included, then the problem is the path you're using with `getResource`. – Slaw Apr 30 '19 at 04:24

0 Answers0