JavaFX java.lang.IllegalStateException: Location is not set
Asked Answered
S

3

6

I'm trying to run my main JavaFX app:

@Override
public void start(Stage primaryStage) throws Exception {
  try {
    FXMLLoader loader = new FXMLLoader();
    loader.setLocation(getClass().getResource("/com/utmkl/fxml/SimulatorDisplay.fxml"));
    Parent content = (Parent)loader.load();

    primaryStage.setResizable(false);
    primaryStage.initStyle(StageStyle.UTILITY);
    primaryStage.setScene(new Scene(content));
    primaryStage.show();

  } catch(Exception e) {
      e.printStackTrace();
  }

This is my folder structure:

Folder structure

Below is the error:

java.lang.IllegalStateException: Location is not set.
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2434)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
    at com.utmkl.VMCSManager.start(VMCSManager.java:43)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)
Soulsearching answered 1/5, 2017 at 15:40 Comment(1)
S
11

I've found the answer..

the resource need to be in the same folder structure as main class

Here is my Maven-JavaFX Folder structure

VMCS
- src/main/java
     - com.utmkl
          VMCSManager.java
- src/main/resources
     - com.utmkl.fxml
          SimulatorDisplay.fxml

So, the correct code (which success to run)

VMCSManager.java

@Override
    public void start(Stage primaryStage) throws Exception {
        try {
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(getClass().getResource("fxml/SimulatorDisplay.fxml"));
            Parent content = loader.load(); 

            Scene scene = new Scene(content);

            primaryStage.setResizable(false);
            primaryStage.initStyle(StageStyle.UTILITY);

            primaryStage.setScene(scene);
            primaryStage.show();            
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
Soulsearching answered 1/5, 2017 at 17:0 Comment(1)
I had to change fxml/nameofview.fmxl to /fxml/nameofview.fxml for it to work.Profanatory
L
3

Even though you do set a location with loader.setLocation(getClass().getResource("/com/utmkl/fxml/ControllerDisplay.fxml")); it is likely that the resource URL doesn't actually refer to an existing resource on the classpath.

If you add System.out.println(getClass().getResource("/com/utmkl/fxml/ControllerDisplay.fxml")); to your code I think it will print null as a result.

Lagging answered 1/5, 2017 at 16:13 Comment(2)
I think it's something to do with maven project structure.. I just don't know how to resolve this at the momentSoulsearching
I don't use Maven but Gradle, but afaik Gradle's default directory structure is the same as Maven's. In src/main/java you should have your Java source, in src/main/resources your resources such as your FXML. If you build your app Maven will combine them. In IDEs such as Eclipse you will need to make sure both directories are marked as source folders, otherwise if you run your application from within your IDE it will not add the resources to the classpath. With Gradle's plugin this is done automatically when importing the Gradle project. I suspect something exists for Maven too.Lagging
D
0

Try loading the resource through the class loader, i.e. instead of class.getResource() use class.getClassLoader().getResource()

Dexedrine answered 18/8, 2024 at 1:10 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.