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.fxmlfilegetResource("/com/John/Smith/Cars/homePage.fxml"). If this does not work you need to check, if the fxml is in the classpath at runtime...src\main\javadirectory? If so, I believe this is your issue. Maven does not copy resources from thesrc\main\javadirectory into the output directory (at least by default). Instead, move your FXML files into thesrc\main\resourcesdirectory. That is where resources (i.e. non-.javafiles) belong. Maven - Introduction to the Standard Directory Layout.