1

I am getting the following error. I have attached my code.

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 com.John.Smith.Cars.Main.start(Main.java:31)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:919)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(PlatformImpl.java:449)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(PlatformImpl.java:418)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:417)
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:175)
... 1 more

Here is my Main class.

package com.John.Smith.Cars;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.concurrent.Task;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.text.Font;
import javafx.stage.Stage;

import java.util.Date;
import java.util.Random;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {

        System.out.println(System.getProperty("java.version"));

        //PushNPull p = new PushNPull();
        //p.populateSQL();

        StageWithData window = new StageWithData(primaryStage);
        FXMLLoader hpLoader = new FXMLLoader(getClass().getResource("/homePage.fxml"));



        System.out.println(hpLoader.getController() + ", " + hpLoader.getLocation());
        Parent root = hpLoader.load(); //This is the line with the error
        ControllerHomePage controller = hpLoader.getController();
        controller.setStage(window);

        Scene scene = new Scene(root);
        scene.getStylesheets().add(getClass().getResource("/styles.css").toExternalForm());
        window.setScene(scene);
        window.show();

        new Thread(() -> {
                abstractSensorDriver sDriver = new SensorDriverTest(window);
                sDriver.startCollection();
        }).start();
    }


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

}

I have seen the other posts with the same error, but still does not seem to work. JAVAFX: Location is not set error "java.lang.IllegalStateException: Location is not set" in JavaFX application

EDIT: Here are the first few lines of my homePage.fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.shape.*?>
<?import javafx.scene.*?>
<?import javafx.scene.text.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.effect.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>

<GridPane alignment="center" hgap="10" stylesheets="@styles.css" vgap="10" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.John.Smith.Cars.ControllerHomePage">
    <rowConstraints>
        <RowConstraints />
        <RowConstraints />
    </rowConstraints>

I have my FXML files in the same folder as my Main class. I am using Maven for this project (my first maven project).

After running the Main method. My println() for the loader location prints out "Null , Null"

5
  • please attach your homePage.fxml file Commented Jul 2, 2018 at 3:09
  • I just attached my homePage.fxml. Commented Jul 2, 2018 at 3:33
  • Should be getResource("/com/John/Smith/Cars/homePage.fxml"). If this does not work you need to check, if the fxml is in the classpath at runtime... Commented Jul 2, 2018 at 7:55
  • When you say, "I have my FXML files in the same folder as my Main class", does that mean the FXML files are in the src\main\java directory? If so, I believe this is your issue. Maven does not copy resources from the src\main\java directory into the output directory (at least by default). Instead, move your FXML files into the src\main\resources directory. That is where resources (i.e. non-.java files) belong. Maven - Introduction to the Standard Directory Layout. Commented Jul 2, 2018 at 8:23
  • @Slaw. That was it. I did not have my FXML files in a resource folder. I had it in my src\main\java directory. After I created a resource folder and added it to my POM file, it worked. Thanks! Commented Jul 2, 2018 at 13:28

1 Answer 1

2

If the file is in the same folder as the class calling the loader, you should not have a / in the file path.

Change the line to:

FXMLLoader hpLoader = new FXMLLoader(getClass().getResource("homePage.fxml"));

You are trying to set the loader to look for that file in the root project folder.

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

1 Comment

I had it working before I used Maven. I moved all of my files to a new Maven project and it is giving me the error now. I am wondering if it has something to do with my folder structure or perhaps my POM file??

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.