Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX java.lang.IllegalStateException: Location is not set

Tags:

javafx

Help! I'm stuck.. I try to run my main javafx app

Here is my codes;

@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();
}

Below is my folder structure: picture 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)
like image 758
Aza Suhaza Avatar asked Feb 04 '23 13:02

Aza Suhaza


1 Answers

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();
        }
    }
like image 156
Aza Suhaza Avatar answered Feb 17 '23 01:02

Aza Suhaza