0

I am working on Java FX project I'm created two FXML file and i want to load another pane to main pain which is Border Pane. I want to load another pane to Border Pane center area!

Here is my entry point of my project that is a Java file

package javafxapplication8;

    import java.io.IOException;
    import javafx.application.Application;
    import javafx.fxml.FXML;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.scene.input.MouseEvent;
    import javafx.scene.layout.BorderPane;
    import javafx.stage.Stage;

    /**
     *
     * @author Anu
     */
    public class JavaFXApplication8 extends Application {

        @Override
        public void start(Stage stage) throws Exception {
            Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
            Scene scene = new Scene(root);
            stage.setScene(scene);
            stage.show();
        }
        @FXML
        void handleButtonAction(MouseEvent event) throws IOException {
        }
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            launch(args);
        }
    }

After that this display the main FXML file which show my Border Pane

<?xml version="1.0" encoding="UTF-8"?>
    <?import com.jfoenix.controls.JFXButton?>
    <?import javafx.scene.layout.AnchorPane?>
    <?import javafx.scene.layout.BorderPane?>

    <AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.171" 
                fx:controller="javafxapplication8.FXMLDocumentController">
       <children>
          <BorderPane fx:id="rootStage" layoutX="14.0" layoutY="-14.0" prefHeight="200.0" prefWidth="320.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
             <left>
                <JFXButton buttonType="RAISED" 
                           onAction="#handleButtonAction"
                            text="Load Pane" BorderPane.alignment="CENTER" />
             </left>
          </BorderPane>
       </children>
    </AnchorPane>

I set a button to display specific pane to that Border Pane center area.

<left>
   <JFXButton buttonType="RAISED" 
    onAction="#handleButtonAction"
    text="Load Pane" BorderPane.alignment="CENTER" />
</left>

This is my Controller class of that Border Pane contained class

package javafxapplication8;

    import java.io.IOException;
    import java.net.URL;
    import java.util.ResourceBundle;
    import javafx.event.ActionEvent;
    import javafx.fxml.FXML;
    import javafx.fxml.FXMLLoader;
    import javafx.fxml.Initializable;
    import javafx.scene.control.Label;
    import javafx.scene.layout.BorderPane;

    /**
     *
     * @author Anu
     */
    public class FXMLDocumentController implements Initializable {

        @FXML
        private BorderPane rootStage;

        @FXML
        private void handleButtonAction(ActionEvent event) throws IOException {
            System.out.println("Button Click");
            FXMLLoader loader = new FXMLLoader();  
            loader.setLocation(javafx.stage.Stage.class.getResource("FXML.fxml"));  

            BorderPane newScene = loader.load();  
            rootStage.setCenter(newScene);  
        }

        @Override
        public void initialize(URL url, ResourceBundle rb) {
            // TODO
            System.out.println("FXMLDocumentController");
        }    

    }

And this is FXML file i want to display in Border Pane center area.

        <?xml version="1.0" encoding="UTF-8"?>
    <?import javafx.scene.control.Label?>
    <?import javafx.scene.layout.AnchorPane?>
    <?import javafx.scene.text.Font?>

    <AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.171" fx:controller="javafxapplication8.FXMLController">
       <children>
          <Label layoutX="252.0" layoutY="179.0" text="Label">
             <font>
                <Font size="41.0" />
             </font>
          </Label>
       </children>
    </AnchorPane>

But when i click Button That give me bunch of errors!

Executing C:\Users\Anu\Documents\NetBeansProjects\JavaFXApplication8\dist\run922593932\JavaFXApplication8.jar using platform C:\Program Files\Java\jdk1.8.0_172\jre/bin/java
    FXMLDocumentController
    Button Click
    Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
        at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
        at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
        at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
        at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
        at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
        at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
        at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
        at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
        at javafx.event.Event.fireEvent(Event.java:198)
        at javafx.scene.Node.fireEvent(Node.java:8411)
        at javafx.scene.control.Button.fire(Button.java:185)
        at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
        at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
        at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
        at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
        at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
        at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
        at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
        at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
        at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
        at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
        at javafx.event.Event.fireEvent(Event.java:198)
        at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
        at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
        at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
        at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$353(GlassViewEventHandler.java:432)
        at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
        at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
        at com.sun.glass.ui.View.notifyMouse(View.java:937)
        at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
        at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
        at java.lang.Thread.run(Thread.java:748)
    Caused by: java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
        at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
        at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
        ... 48 more
    Caused by: java.lang.IllegalStateException: Location is not set.
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2434)
        at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
        at javafxapplication8.FXMLDocumentController.handleButtonAction(FXMLDocumentController.java:33)
        ... 58 more

My project structure:

Project structure screenshot

Can anybody show me where i am missed? Thank you.

fabian
  • 80,457
  • 12
  • 86
  • 114
Sahan Pasindu Nirmal
  • 433
  • 4
  • 13
  • 36
  • i cannot fine the solution on there. it is complex – Sahan Pasindu Nirmal Jul 04 '18 at 06:52
  • no sir still same the error – Sahan Pasindu Nirmal Jul 04 '18 at 06:57
  • 1
    Basically, your `javafx.stage.Stage.class.getResource("FXML.fxml")` call is returning `null` which means the "location is not set". When you call `Class.getResource(String)` with a `String` that does not have a leading `/` it looks for the resource relative to the `Class`(in your case, `Stage`). As it is incredibly unlikely that you're storing your FXML files inside JavaFX packages the resource doesn't exist. You should be calling `getClass().getResource(String)` to locate the resource relative to the calling class (i.e. `FXMLDocumentController`). Either that, or use an absolute resource name. – Slaw Jul 04 '18 at 06:58
  • Since there's no info about where the file/jar structure in your classpath, it's impossible to come up with the correct path... – fabian Jul 04 '18 at 07:00
  • sir my only package is javafxapplication8 this one – Sahan Pasindu Nirmal Jul 04 '18 at 07:01
  • FXMLLoader loader = new FXMLLoader(); loader = FXMLLoader.load(getClass().getResource("FXML.fxml")); i use this code. but still same the error – Sahan Pasindu Nirmal Jul 04 '18 at 07:02
  • How is your project structured? What are you using to build and run your project (Pure IDE or a tool like Ant, Maven, Gradle, etc...)? Are you sure the `FXML.fxml` file is copied to the output and present on the classpath? Is the resource in the same package as `FXMLDocumentController` or another package? – Slaw Jul 04 '18 at 07:09
  • Sir i am using Netbeans and all the class and FXML are in the same package javafxapplication8 – Sahan Pasindu Nirmal Jul 04 '18 at 07:10
  • https://imgur.com/a/5n8pnnk my structure – Sahan Pasindu Nirmal Jul 04 '18 at 07:12
  • Just created a project based on your structure and code in NetBeans via `File->New Project->JavaFX->JavaFX Application`. After changing `Stage.class.getResource(...)` to `getClass().getResource(...)` I **did not** get the same exception. The original exception was a `NullPointerException` whereas the new exception is a `ClassCastException`. You are calling `BorderPane newScene = loader.load()` but the root node in `FXML.fxml` is an `AnchorPane`. And, as expected, a `AnchorPane` cannot be cast to a `BorderPane`. – Slaw Jul 04 '18 at 07:30
  • When dealing with `InvocationTargetException`s you have to look at what the actual _cause_ of the exception is (i.e. the last `Caused by:`). To read more about how to understand stack traces look at this question: https://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors – Slaw Jul 04 '18 at 07:33
  • (_Correction: original exception is an `IllegalStateException`_). Anyway, after changing `BorderPane newScene = loader.load()` to `AnchorPane newScene = loader.load()` the application ran without errors. Though I did need to resize the window to see the newly added `Label`. – Slaw Jul 04 '18 at 07:37
  • FXMLLoader loader = new FXMLLoader(); loader.setLocation(javafx.stage.Stage.class.getResource("/javafxapplication8/FXML.fxml")); AnchorPane newScene = loader.load(); rootStage.setCenter(newScene); this is not worked either sir – Sahan Pasindu Nirmal Jul 04 '18 at 08:01

1 Answers1

2

There are 2 reasons

javafx.stage.Stage.class.getResource("FXML.fxml")

returns null which is the cause for the exception:

  • Resources not starting with / are resolved relative to the class, i.e. the resource java is looking for in this case is /javafx/stage/FXML.fxml instead of the real location of your resouce
  • Stage for some reason seems to use a different classloader that cannot resolve resources located in the jar. Make sure to use a different class that is loaded by a classloader that has access to the jar, e.g. using FXMLDocumentController.class
loader.setLocation(FXMLDocumentController.class.getResource("/javafxapplication8/FXML.fxml"));

Note: You may need to use Clean and Build to get the new .class file to the jar.

fabian
  • 80,457
  • 12
  • 86
  • 114
  • finally this loader.setLocation(FXMLDocumentController.class.getResource("/javafxapplication8/FXML.fxml")); piece of code save my day sir. i need to say thank you so much. and i need to say more. but comments are not allow to it, it is used for only improve the answers. :) – Sahan Pasindu Nirmal Jul 04 '18 at 08:59