java.lang.IllegalStateException: Location is not set
java.lang.IllegalStateException: Location is not set
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"
homePage.fxml
I just attached my homePage.fxml.
– ZooHe17
Jul 2 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...– fabian
Jul 2 at 7:55
getResource("/com/John/Smith/Cars/homePage.fxml")
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
srcmainjava
directory? If so, I believe this is your issue. Maven does not copy resources from the srcmainjava
directory into the output directory (at least by default). Instead, move your FXML files into the srcmainresources
directory. That is where resources (i.e. non-.java
files) belong. Maven - Introduction to the Standard Directory Layout.– Slaw
Jul 2 at 8:23
srcmainjava
srcmainjava
srcmainresources
.java
@Slaw. That was it. I did not have my FXML files in a resource folder. I had it in my srcmainjava directory. After I created a resource folder and added it to my POM file, it worked. Thanks!
– ZooHe17
Jul 2 at 13:28
1 Answer
1
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"));
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.
Incidentally, you may have the same problem later when setting the stylesheet...
– Zephyr
Jul 2 at 3:15
I tried that and still recieved the same error.
– ZooHe17
Jul 2 at 3:27
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??
– ZooHe17
Jul 2 at 3:28
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
please attach your
homePage.fxml
file– xxy
Jul 2 at 3:09