0

I want to run my application, but an exception returns to me. I found a similar question and did it the same way, but it did not help me. I think the mistake is stupid, but I don't see it, and I don't understand what to do, please help.

my main method

public class App extends Application {
    private Stage menuBar;
    private BorderPane mainWindow;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        this.menuBar = primaryStage;

        initMenuWindow();
    }

    private void initMenuWindow() {
        try {
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(App.class.getResource("menuWindow.fxml"));
            mainWindow = loader.load();     //ERROR

            Scene scene = new Scene(mainWindow);
            menuBar.setScene(scene);

            menuBar.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

and exception

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
    at java.base/java.lang.Thread.run(Thread.java:835)
Caused by: java.lang.IllegalStateException: Location is not set.
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2459)
    at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2435)
    at java/zherikhov.App.initMenuWindow(App.java:48)
    at java/zherikhov.App.start(App.java:27)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:389)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    ... 1 more
Exception running application zherikhov.App

my java module

module java {
    opens zherikhov to javafx.fxml;
    exports zherikhov;
    requires javafx.fxml;
    requires javafx.graphics;
    requires javafx.controls;
}

[screenshot [screenshot

  • My guess is the path to the fxml file in `loader.setLocation(App.class.getResource("menuWindow.fxml"));`. Try `"/menuWindow.fxml"` and other options... See [this question](https://stackoverflow.com/questions/17228487/javafx-location-is-not-set-error-message). – deHaar Jul 11 '19 at 05:22
  • @deHaar `/menuWindow.fxml` is probably the first thing I tried) and I also experimented with the paths, but I agree, I think the problem is somewhere here – Vlad Zherihov Jul 11 '19 at 05:32
  • 1
    This has been asked and answered dozens of times. Please try the search feature and search for "Location not set." – Zephyr Jul 11 '19 at 05:40
  • What is the command you use to execute your application? – Slaw Jul 11 '19 at 06:47
  • `/menuWindow.fxml` seems the incorrect path. Paths relative to the class seemed to stop working at some time for my IDE/JDK, but the complete path continues to work. Have you tried `/zherikhov/menuWindow.fxml`? – fabian Jul 11 '19 at 06:50
  • @fabian yes i tried – Vlad Zherihov Jul 11 '19 at 09:48
  • @Slaw If I understand you correctly, then I work at `IntelliJ IDEA` and use the `Run App` to run. – Vlad Zherihov Jul 11 '19 at 09:53
  • Can you check your `target/classes` directory to see if the resources are being copied to the output? – Slaw Jul 11 '19 at 11:58
  • @Slaw it turns out no, I attached a screenshot – Vlad Zherihov Jul 11 '19 at 12:47
  • 1
    Then that's the source of the problem. Unfortunately, as you're using the IDE directly to build and run your project (rather than delegating to Maven), I'm not sure how to fix it. I would have assumed the problem was caused by IntelliJ not recognizing the `src/main/resources` directory as a resources root, but the directory's icon would indicate otherwise (you may want to still check this to be sure). If you run `mvn compile` are the resources copied? – Slaw Jul 11 '19 at 18:10
  • try `/zherikhov/menuwindow.fxml` , it should work (most probably, 'cause of the location of the fxml) – Ryotsu Jul 12 '19 at 04:34
  • @Ryotsu I tried, in fact, I think the problem is somewhere else, I just don't have enough experience to pay attention to it – Vlad Zherihov Jul 12 '19 at 04:37
  • @VladZherihov yeah, I tired your code too with your directory structure and it seems to be working well but I used Java 8, seems like the fxml's are missing in your maven generated output – Ryotsu Jul 12 '19 at 04:48
  • @Ryotsu I don't know what the reason was, but I just re-created the project and it all worked – Vlad Zherihov Jul 13 '19 at 09:33
  • @Slaw I don't know what the reason was, but I just re-created the project and it all worked – Vlad Zherihov Jul 13 '19 at 09:33

1 Answers1

-1

Your problem looks simple, Just replace the FXML file name menuWindow.fxml with the full path of the file starting at the src file, ending with the filename with the .fxml

I hope this works for you ( it worked in my case but your project's structure looks different)

Ibraheem Alyan
  • 426
  • 1
  • 5
  • 14