Reputation: 23
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"
Upvotes: 1
Views: 2675
Reputation: 10253
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.
Upvotes: 2