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

15
  • 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. Commented Jul 11, 2019 at 5: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 Commented Jul 11, 2019 at 5:32
  • 1
    This has been asked and answered dozens of times. Please try the search feature and search for "Location not set." Commented Jul 11, 2019 at 5:40
  • What is the command you use to execute your application? Commented Jul 11, 2019 at 6: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? Commented Jul 11, 2019 at 18:10

1 Answer 1

-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)

Sign up to request clarification or add additional context in comments.

1 Comment

I've probably tried all the different options for the paths, otherwise I would not have come here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.