0

This is my directory tree:

enter image description here I am not able to load theese fxml from their location in my code.

For example, in Main.java

FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("MainInterface.fxml"));
Scene scene = new Scene(loader.load());
primaryStage.setScene(scene);
primaryStage.show();

but this goes into an error:

Exception in thread "main" java.lang.RuntimeException... Caused by: java.lang.IllegalStateException: Location is not set.

How do I do that?

Thank you all.

user2640732
  • 39
  • 1
  • 7
  • 1
    Try either `Main.class.getResource("/MainInterface.fxml")` (with the leading `/`), or `Main.class.getClassLoader().getResource("MainInterface.fxml")`. If those don't work, [edit] your question to show the actual layout of your build folder (not the source folder you have shown). I'm not familiar with your IDE, but I think `out` is the build folder. – James_D Mar 22 '16 at 16:56
  • As JamesD suggested the leading / should work or use the same build structure within your resources folder as you do in src and put the fxml files in the "same" directory as the class you are loading that fxml with – JohnRW Mar 22 '16 at 17:13
  • @JamesD why are you never posting your answers as answer? :D – JohnRW Mar 22 '16 at 17:14
  • 1
    @JohnRW Because in this case I don't know that it's the correct answer, since I don't know the OP's build scenario. Maybe the build scenario results in a `resources` package containing the FXML files, in which case the answer will not work. Or maybe the build process doesn't export the FXML files (which happens sometimes with IntelliJ, I believe). Because there's not really enough information in the question, I don't know if this is the correct answer. – James_D Mar 22 '16 at 17:19
  • Makes sense. Thanks for enlightening me – JohnRW Mar 22 '16 at 17:20
  • 2
    Trust me - I post [plenty of answers](http://stackoverflow.com/users/2189127/james-d?tab=answers) – James_D Mar 22 '16 at 17:22
  • Possible duplicate of [Get a resource using getResource()](http://stackoverflow.com/questions/2593154/get-a-resource-using-getresource) – fabian Mar 22 '16 at 17:53

0 Answers0