0

Hi I have problem with open new window fxml view after click button. Main window display, in main view i have six buttons. I'd like to i click button opened new window. However while I click button, exception is displayed:
java.lang.reflect.InvocationTargetException

I try different solutions found in internet but display this exception. Mainly i try modify code in my switch function, responsible for display new view. I think that problem is here or code in main class.

Main class:
    public class Main extends Application {
         public void start(Stage stage) throws Exception {
           AnchorPane pane =   FXMLLoader.load(getClass().getResource("/mainView.fxml"));
        Scene scene = new Scene(pane);
        stage.setScene(scene);
        stage.show();
    }

    @Override
    public void init() {
        System.out.println("Init");
    }

    @Override
    public void stop() {
        System.out.println("Stop");
    }


    public static void main(String[] args) {

        launch(args);
    }
}
MainController.java
public class MainController {

    @FXML
    private Button addChangeMainButton;

    @FXML
    private Button viewChangesButton;

    @FXML
    private Button addChangeBoughtButton;

    @FXML
    private Button viewContractsButton;

    @FXML
    private Button addContractButton;

    @FXML
    private Button viewStatisticsButton;




    @FXML
    void chooseViewAction(ActionEvent event) throws IOException {
        Button button = (Button) event.getSource();
        String buttonId = button.getId();
        MainLogic mainl = new MainLogic();
        mainl.chooseView(buttonId);

    }

}
public class MainLogic {
   private String chooseView;



   public void chooseView(String chooseView) throws IOException {
       switch (chooseView) {
           case "addChangeMainButton":
               FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("addChangeVIEW.fxml"));
               Parent root1 = fxmlLoader.load();
               Stage stage = new Stage();
               stage.setScene(new Scene(root1));
               stage.show();

               break;

           case "viewChangesButton":
               System.out.println("viewChangesButton");

               break;

           case "addChangeBoughtButton":
               System.out.println("addChangeBoughtButton");

               break;
           case "viewContractsButton":
               System.out.println("viewContractsButton");

               break;

           case "addContractButton":
               System.out.println("addContractButton");
               break;

           case "viewStatisticsButton":
               System.out.println("viewStatisticsButton");
               break;

               default:
                   System.out.println("default");
                   break;

       }
   }


}
mainView.fxml
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.controller.MainController">
   <children>
      <Button fx:id="addChangeMainButton" layoutX="31.0" layoutY="45.0" mnemonicParsing="false" onAction="#chooseViewAction" prefHeight="70.0" prefWidth="150.0" text="Add Change" />
      <Button fx:id="viewChangesButton" layoutX="31.0" layoutY="283.0" mnemonicParsing="false" onAction="#chooseViewAction" prefHeight="70.0" prefWidth="150.0" text="View Changes" />
      <Button fx:id="addChangeBoughtButton" layoutX="217.0" layoutY="45.0" mnemonicParsing="false" onAction="#chooseViewAction" prefHeight="70.0" prefWidth="150.0" text="Change Bought/Sold" />
      <Button fx:id="viewContractsButton" layoutX="225.0" layoutY="283.0" mnemonicParsing="false" onAction="#chooseViewAction" prefHeight="70.0" prefWidth="150.0" text="View Contracts" />
      <Button fx:id="addContractButton" layoutX="403.0" layoutY="45.0" mnemonicParsing="false" onAction="#chooseViewAction" prefHeight="70.0" prefWidth="150.0" text="Add Contract" />
      <Button fx:id="viewStatisticsButton" layoutX="403.0" layoutY="283.0" mnemonicParsing="false" onAction="#chooseViewAction" prefHeight="70.0" prefWidth="150.0" text="View Statistics" />
   </children>
</AnchorPane>

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>


<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.controller.addChangeController">
   <children>
      <Button fx:id="button" layoutX="263.0" layoutY="210.0" mnemonicParsing="false" text="Button" />
   </children>
</AnchorPane>

addChangeController.java
package com.controller;

import javafx.fxml.FXML;
import javafx.scene.control.Button;

public class addChangeController {

    @FXML
    private Button button;

}

module-info.java

module ProjectCPApril {
    requires javafx.graphics;
    requires javafx.fxml;
    requires javafx.controls;
    exports com.controller to javafx.graphics;
    opens com.controller to javafx.fxml;
    exports com.logic to javafx.graphics;
    opens com.logic to javafx.fxml;

}
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1787)
    at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1670)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    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:8879)
    at javafx.controls/javafx.scene.control.Button.fire(Button.java:200)
    at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:206)
    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:218)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    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:3851)
    at javafx.graphics/javafx.scene.Scene$MouseHandler.access$1200(Scene.java:3579)
    at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1849)
    at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2588)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:397)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:434)
    at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:390)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:433)
    at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)
    at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)
    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:174)
    at java.base/java.lang.Thread.run(Thread.java:834)
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:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76)
    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:566)
    at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:273)
    at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83)
    at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1782)
    ... 47 more
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 ProjectCPApril/com.logic.MainLogic.chooseView(MainLogic.java:27)
    at ProjectCPApril/com.controller.MainController.chooseViewAction(MainController.java:43)
    ... 58 more
Stop

Process finished with exit code 0
David W
  • 51
  • 2
  • 8
  • Can you provide the fxml files as well – Sai Dandem Apr 07 '19 at 22:10
  • I attached two files .fxml – David W Apr 07 '19 at 22:21
  • I couldnt find any error with the demo, except that you have not provided fx:controller="com.controller.addChangeController" in addChangeVIEW.fxml. Can you provide the addChangeController.java – Sai Dandem Apr 07 '19 at 22:37
  • Ok, I added also module-info.java because i use sdk 11 – David W Apr 07 '19 at 23:06
  • Ok. I am checking with jdk8 and everything is workign well with what you have given. And sorry that I dont have a working environment for jdk 11.. may be some one will look into this. Good luck :) – Sai Dandem Apr 07 '19 at 23:12
  • https://github.com/sedj601/RestaurantOrdersDuplicateFX – SedJ601 Apr 08 '19 at 00:45
  • 4
    The exception is telling you the location is not set on the `FXMLLoader`. This means `getClass().getResource(...)` is return null and that means the resource could not be found at the specified location. Where is `addChangeVIEW.fxml` relative to the `MainLogic` class? Because if it's not in the same package the given path won't work. – Slaw Apr 08 '19 at 01:47
  • `addChangeVIEW.fxml` is in folder `resources`, `MainLogic.java` is in package 'com.logic' and all files `*Controller.java are com.controller` – David W Apr 08 '19 at 15:12
  • If `resources` is the root of the classpath, try using `"/addChangeVIEW.fxml"` (notice the `/`, that makes the path absolute). If `resources` is a package, then try `"/resources/addChangeView.fxml"`. For more information, see the [documentation of `Class#getResource(String)`](https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/lang/Class.html#getResource(java.lang.String)). – Slaw Apr 08 '19 at 15:18
  • first path solved my problem :) Thank you very much :) – David W Apr 08 '19 at 15:22
  • and your question Help me a lot, Thanks, Sometimes question helps people much. – Noor Hossain Jun 26 '21 at 22:03

1 Answers1

1

Copying information from comments into an answer

The important part of the stack trace is:

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 ProjectCPApril/com.logic.MainLogic.chooseView(MainLogic.java:27)
    at ProjectCPApril/com.controller.MainController.chooseViewAction(MainController.java:43)
    ... 58 more

That tells you that the FXMLLoader's location is null when you call load. As you're setting it via Class.getResource that means said method is returning null. When getResource returns null that means the resource could not be found with the given path.

You're currently using "addChangeVIEW" as the path, but you say:

addChangeVIEW.fxml is in folder resources, MainLogic.java is in package com.logic

Since your path has no leading / it is relative to the Class you call getResource on. Since you use MainLogic.class the path gets resolved to com/logic/addChangeVIEW.fxml—that is not where you have the resource. You've put the resource at the root of the classpath, which can retrieved by using an absolute path: "/addChangeVIEW.fxml".

See Class.getResource(String) for more information.

Slaw
  • 37,820
  • 8
  • 53
  • 80