0

I need help with java code, I have a few buttons on my main stage, I am trying to code back-office button action right now, the theory is when the back-office button is clicked it should pop up a second scene. However, it spits out java.lang.reflect.InvocationTargetException error.

Here is my main class

package pos_prototype.pos_prototype;

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

import java.io.IOException;
import java.util.Objects;

public class mainPage extends Application {
    @Override
    public void start(Stage stage) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(mainPage.class.getResource("mainPage.fxml"));
        Scene scene = new Scene(fxmlLoader.load(), 1300, 900);
        stage.setTitle("Title");
        stage.setScene(scene);
        stage.show();
    }

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

Main Controller (This file are in a package call controller)

package pos_prototype.pos_prototype.Controller;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.stage.Stage;


import java.io.IOException;



public class mainPageController {

    @FXML
    protected void openCarryOut(){
        Alert alert = new Alert(Alert.AlertType.CONFIRMATION, "Works");
        alert.showAndWait();
    }

    @FXML
    protected void openDineIn(){
        Alert alert = new Alert(Alert.AlertType.CONFIRMATION, "Works");
        alert.showAndWait();
    }

    @FXML
    protected void openOrder(){
        Alert alert = new Alert(Alert.AlertType.CONFIRMATION, "Works");
        alert.showAndWait();
    }

    @FXML
    protected void openBackOffice() throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("backOffice.fxml"));
        Scene scene = new Scene(fxmlLoader.load(), 1300, 900);
        Stage stage = new Stage();
        stage.setTitle("backOffice");
        stage.setScene(scene);
        stage.show();
    }
}

Here is the fxml file

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

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>


<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="900.0" prefWidth="1300.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="pos_prototype.pos_prototype.Controller.mainPageController">
   <children>
      <Label alignment="CENTER" layoutX="73.0" layoutY="77.0" prefHeight="160.0" prefWidth="1155.0" text="DreamAwake POS">
         <font>
            <Font size="96.0" />
         </font>
      </Label>
      <VBox layoutX="101.0" layoutY="350.0" prefHeight="468.0" prefWidth="1139.0">
         <children>
            <HBox prefHeight="156.0" prefWidth="371.0">
               <children>
                  <Button mnemonicParsing="false" onAction="#openCarryOut" prefHeight="156.0" prefWidth="247.0" text="Carry Out">
                     <font>
                        <Font size="24.0" />
                     </font>
                  </Button>
                  <Label prefHeight="221.0" prefWidth="39.0" />
                  <Button mnemonicParsing="false" onAction="#openDineIn" prefHeight="156.0" prefWidth="247.0" text="Dine In">
                     <font>
                        <Font size="24.0" />
                     </font>
                  </Button>
                  <Label prefHeight="221.0" prefWidth="39.0" />
                  <Button mnemonicParsing="false" onAction="#openOrder" prefHeight="156.0" prefWidth="247.0" text="Order">
                     <font>
                        <Font size="24.0" />
                     </font>
                  </Button>
                  <Label prefHeight="221.0" prefWidth="39.0" />
                  <Button mnemonicParsing="false" onAction="#openBackOffice" prefHeight="156.0" prefWidth="247.0" text="Back Office">
                     <font>
                        <Font size="24.0" />
                     </font>
                  </Button>
               </children>
               <VBox.margin>
                  <Insets />
               </VBox.margin>
            </HBox>
            <HBox prefHeight="100.0" prefWidth="200.0" />
            <HBox prefHeight="156.0" prefWidth="371.0">
               <children>
                  <Button mnemonicParsing="false" prefHeight="156.0" prefWidth="247.0" text="Unused" />
                  <Label prefHeight="221.0" prefWidth="39.0" />
                  <Button mnemonicParsing="false" prefHeight="156.0" prefWidth="247.0" text="Unused" />
                  <Label prefHeight="221.0" prefWidth="39.0" />
                  <Button mnemonicParsing="false" prefHeight="156.0" prefWidth="247.0" text="Unused" />
                  <Label prefHeight="221.0" prefWidth="39.0" />
                  <Button mnemonicParsing="false" prefHeight="156.0" prefWidth="247.0" text="Unused" />
               </children>
            </HBox>
         </children>
      </VBox>
   </children>
</AnchorPane>

The problem here is that when I click on the back-office button this error shows up

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1857)
    at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1724)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
    at javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8792)
    at javafx.controls/javafx.scene.control.Button.fire(Button.java:203)
    at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:208)
    at javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:247)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
    at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3897)
    at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1878)
    at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2623)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:411)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:301)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:450)
    at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:424)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:449)
    at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:557)
    at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:943)
    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:184)
    at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:77)
    at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:84)
    at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1854)
    ... 46 more
Caused by: java.lang.IllegalStateException: Location is not set.
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2541)
    at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2516)
    at pos_prototype.pos_prototype/pos_prototype.pos_prototype.Controller.mainPageController.openBackOffice(mainPageController.java:39)
    ... 57 more

Process finished with exit code 0
  • 1
    Ensure you search for Java naming conventions and apply them to any code you share online. I advise you do that now with the code in your question. – jewelsea Dec 10 '21 at 05:53
  • 1
    The comments as well as the troubleshooting section of the duplicate and the [eden resource guide](https://edencoding.com/where-to-put-resource-files-in-javafx/) should help you fix your issues, I believe. – jewelsea Dec 10 '21 at 05:58
  • 1
    Make sure you understand how to read the cause of stack traces so you can appropriately search for solutions. `Caused by: java.lang.IllegalStateException: Location is not set` has many solutions on the net. – jewelsea Dec 10 '21 at 06:58
  • 1
    For the point in the last comment ("Make sure you understand how to read the cause of stack traces"), see https://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors – James_D Dec 10 '21 at 13:00

0 Answers0