0

I'm trying to load an FXML for my App just like I did in another project. Difference: doesn't work. I get an "IllegalStateException: Location is required"

I tried to move the file to a different location but that didn't work either. I checked the URL (tmp) with the debugger. It points to the correct file.

    URL tmp = getClass().getClassLoader().getResource("FXMLDocument.fxml");
    FXMLLoader baseLoader = new FXMLLoader(tmp);
    Parent root = baseLoader.load();
    Scene scene = new Scene(root);        
    stage.setScene(scene);
    stage.show();
latteyo
  • 1
  • 1
  • Please add the stack trace to the question. What is the location of the `FXMLDocument.fxml` relative to the class? – Boris Oct 04 '19 at 09:49
  • 2
    According to the stack trace, it's located `FXMLDocument.fxml` just fine. The problem is in the `FXMLDocumentController#initialize` method, on line `43`. You must be attempting to load another FXML file on that line and that's the FXML file causing the problems. Please show that part of your code, as well as the structure of your project (i.e. please provide a [mre]). – Slaw Oct 04 '19 at 10:53

2 Answers2

0

The code that was actually causing the problems was not the one in the question.

As it turns out

URL tmp = getClass().getResource("FXMLDrawerMain.fxml");

returned null for whatever reason. (Maybe OS?)

I replaced it with

URL tmp = getClass().getClassLoader().getResource("FXMLDrawerMain.fxml");

and now it just works fine.

latteyo
  • 1
  • 1
  • 1
    _code that was actually causing the problems was not the one in the question_ that's one reason you should provide an [mcve] (vs. mere snippets) No, OS has nothing to do with it - please read the api doc of getResource for both class and classLoader (hint: they resolve the given path differently). Typically, it's not recommended to have anything in the default package (aka: no package) .. better cleanup your project structure :) – kleopatra Oct 07 '19 at 14:58
  • If `getClass().getClassLoader("FXMLDrawerMain.fxml")` works then so should `getClass().getResource("/FXMLDrawerMain.fxml")` (notice the leading `/`). – Slaw Oct 10 '19 at 22:06
-1

Please, replace:

URL tmp = getClass().getClassLoader().getResource("FXMLDocument.fxml");

to

URL tmp = getClass().getResource("FXMLDocument.fxml");
Dmitry Rakovets
  • 557
  • 1
  • 6
  • 15
  • 1
    trying random paths without explaining why/what you are doing isn't overly helpful ... the suggestion in your comment is the exact same lookup as the original ... (classloader _always_ assumes the package path to be absolute) – kleopatra Oct 04 '19 at 11:46