faraaz
faraaz

Reputation: 128

"java.lang.IllegalStateException: Location is not set" in JavaFX application

I am making an application for an accounting firm using JavaFX. I am getting a strange error when clicking a button I have set via FXML.

java.lang.IllegalStateException: Location is not set.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2434)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)

Here is my Main.java code:

    package tech.faraaz.zoforo;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("gui/SplashScreen.fxml"));

        Scene scene = new Scene(root);

        stage.setTitle("Zoforo");
        stage.setScene(scene);
        stage.show();
    }


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

Here is my splash screen code:

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane maxHeight="410.0" maxWidth="410.0" minHeight="410.0" minWidth="410.0" prefHeight="400.0" prefWidth="401.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="tech.faraaz.zoforo.gui.SplashScreenController">
   <children>
      <ImageView fitHeight="150.0" fitWidth="200.0" layoutX="24.0" layoutY="23.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@../assets/logo.png" />
         </image>
      </ImageView>
      <ListView layoutX="24.0" layoutY="158.0" prefHeight="200.0" prefWidth="200.0" />
      <Button layoutX="248.0" layoutY="172.0" mnemonicParsing="false" onMouseClicked="#openAccountWindow" prefHeight="27.0" prefWidth="139.0" text="Open Account" />
      <Button layoutX="248.0" layoutY="209.0" mnemonicParsing="false" prefHeight="27.0" prefWidth="139.0" text="New Account" />
      <Button layoutX="248.0" layoutY="313.0" mnemonicParsing="false" prefHeight="27.0" prefWidth="139.0" text="Scan Statement" />
      <Button layoutX="248.0" layoutY="276.0" mnemonicParsing="false" prefHeight="27.0" prefWidth="139.0" text="Open Statement" />
      <Label layoutX="24.0" layoutY="374.0" text="Copyright Zoforo 2017 | zoforo.com" />
      <TextField layoutX="24.0" layoutY="122.0" promptText="Search Profiles..." />
   </children>
</AnchorPane>

Here is the code in SplashScreenController.java. I am using "Event" instead of "ActionEvent".

    @FXML
public void openAccountWindow(Event event) throws Exception {

    try {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("gui/OpenAccountScreen.fxml"));
        Parent root = (Parent) loader.load();

        Stage stage = new Stage();
        stage.setScene(new Scene(root));
        stage.show();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Here is the code for OpenAccountScreen.java.

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>


<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="384.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <ImageView fitHeight="71.0" fitWidth="169.0" layoutX="14.0" layoutY="14.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@../assets/logo.png" />
         </image>
      </ImageView>
      <TableView layoutX="14.0" layoutY="137.0" prefHeight="200.0" prefWidth="572.0">
        <columns>
          <TableColumn prefWidth="204.0" text="Account Name" />
          <TableColumn minWidth="0.0" prefWidth="218.0" text="Account Number" />
            <TableColumn minWidth="8.0" prefWidth="149.0" text="Statements" />
        </columns>
      </TableView>
      <Button layoutX="480.0" layoutY="346.0" mnemonicParsing="false" text="Open Account" />
      <TextField layoutX="14.0" layoutY="101.0" promptText="Search Accounts..." />
   </children>
</AnchorPane>

Any help is appreciated. Thanks.

Upvotes: 2

Views: 11279

Answers (1)

jewelsea
jewelsea

Reputation: 159386

In your Main class, your package and class name is:

tech.faraaz.zoforo.Main

In SplashScreenController it is:

tech.faraaz.zoforo.gui.SplashScreenController

So these classes are in different packages.

Yet you try to get the resource using the same relative location.

In Main:

getClass().getResource("gui/SplashScreen.fxml")

In SplashScreenController:

getClass().getResource("gui/OpenAccountScreen.fxml")

So relative to the location of the SplashController, then OpenAccountScreen.fxml would need to be in the following location for it to be found:

tech/faraaz/zoforo/gui/gui/OpenAccountScreen.fxml

I bet it's not there...

Probably, rather than accessing the FXML relative to the current class, you should access it relative to a given class (e.g. always Main) or via absolute references. That might help prevent your confusion.

For example, write:

Main.class.getResource("gui/SplashScreen.fxml");
Main.class.getResource("gui/OpenAccountScreen.fxml");

OR:

getClass().getResource("/tech/faraaz/zoforo/gui/SplashScreen.fxml")
getClass().getResource("/tech/faraaz/zoforo/gui/OpenAccountScreen.fxml")

Note, to debug stuff like this, you can always just run:

System.out.println(getClass().getResource("gui/OpenAccountScreen.fxml"));

If it prints null, then you know the resource is not in the location you expect, then you can troubleshoot from there.

Upvotes: 13

Related Questions