I am following this tutorial to get into JavaFX using scene builder and so far I really like it as the person who made it explains things in deep detail. I finished the first part and tried executing the code so far, but it returns multiple errors. Here is more information about the project.

The project is based on Model-View-Controller structure, therefore, I have the following packages -
slavi.address.controller
slavi.address.view
slavi.address.model

The view package, contains 2 files, 1 for Root Layout and 1 with the application layout(pretty basic app).
The files in it are PersonOverview.fxml and RootLayout.fxml

In the controller package I have the following file(Provided by the tutorial)

package slavi.address.controller;


import java.io.IOException;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class MainApp extends Application {

    private Stage primaryStage;
    private BorderPane rootLayout;

    @Override
    public void start(Stage primaryStage) {
        this.primaryStage = primaryStage;
        this.primaryStage.setTitle("AddressApp");

        try {
            // Load the root layout from the fxml file
            FXMLLoader loader = new FXMLLoader(MainApp.class.getResource("view/RootLayout.fxml"));
            rootLayout = (BorderPane) loader.load();
            Scene scene = new Scene(rootLayout);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch (IOException e) {
            // Exception gets thrown if the fxml file could not be loaded
            e.printStackTrace();
        }

        showPersonOverview();
    }

    /**
     * Returns the main stage.
     * @return
     */
    public Stage getPrimaryStage() {
        return primaryStage;
    }

    /**
     * Shows the person overview scene.
     */
    public void showPersonOverview() {
        try {
            // Load the fxml file and set into the center of the main layout
            FXMLLoader loader = new FXMLLoader(MainApp.class.getResource("view/PersonOverview.fxml"));
            AnchorPane overviewPage = (AnchorPane) loader.load();
            rootLayout.setCenter(overviewPage);

        } catch (IOException e) {
            // Exception gets thrown if the fxml file could not be loaded
            e.printStackTrace();
        }
    }

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

The errors I get are the following:

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.access$000(Unknown Source)
    at com.sun.javafx.application.LauncherImpl$1.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IllegalStateException: Location is not set.
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.load(Unknown Source)
    at application.Main.start(Main.java:26)
    at com.sun.javafx.application.LauncherImpl$8.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$7.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$6$1.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$6$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl$6.run(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.access$300(Unknown Source)
    at com.sun.glass.ui.win.WinApplication$4$1.run(Unknown Source)
    ... 1 more
Exception running application application.Main

I think there is something wrong with the provided path as well, because on the tutorial instead of slavi.address.controller they suggest slavi.address

Recommended Answers

All 2 Replies

Caused by: java.lang.IllegalStateException: Location is not set.
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.load(Unknown Source)

... seems to suggest there's something missing in your scenebuilder FXML file?

Update: It seems this can be caused by the FXML file not being where you told the loader it is. In turn, frequently caused by misunderstanding the path where Java looks for files.

Off the top of my head (no access to javadoc right now), MainApp.class.getResource("view/RootLayout.fxml") wants a RootLayout.fxml file inside a view folder inside the folder where MainApp's class file was.
I would normally pass the absolute path, starting from the applications root folder. eg "/slavi/address/view/RootLayout.fxml" (note initial slash)

Hey James, thanks for responding.
That solved my issue! I tried earlier the same without the initial slash, but thanks for pointing it out

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.