2

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.

faraaz
  • 126
  • 1
  • 2
  • 11
  • 2
    This has been asked before, please google: "location not set javafx". The actual error is likely in code you don't show (for example `tech.faraaz.zoforo.gui.SplashScreenController`, or some code that it triggers on button click). Also, you get the error "clicking a button", which button? (I guess that it is "Open Account") and the error is generated in `openAccountWindow`. – jewelsea Jul 25 '17 at 23:40
  • Here is the code in the SplashScreenController. I already checked that question before hand and couldn't find a fix. @jewelsea `@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(); } }` – faraaz Jul 25 '17 at 23:53
  • 1
    Please edit the question and put the code in the question rather than in a comment. Also, looking at the code you provided, the error is probably in OpenAccountScreen.fxml or it's controller, so you will probably need to provide those as well. The best way to get quick answers for these kinds of issues is to provide an [mcve] that somebody could copy and paste to immediate replicate the issue without any changes. If you have already checked a related question or resource and it didn't solve you issue, also note that in your question (with a link). – jewelsea Jul 26 '17 at 00:07
  • @jewelsea Okay, I have edited the post with the sufficient code. Thank you for the tips. – faraaz Jul 26 '17 at 00:54

1 Answers1

12

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.

jewelsea
  • 150,031
  • 14
  • 366
  • 406