0

I am working with javaFX on inteliJ using javafx-sdk-11.0.2. I made a GUI for a cinema and when I want to run it on inteliJ, everything works fine. But when I create a jar file and I execute it then everything works fine again until I click on a button for switching from scene.

That is what I get when clicking on the button. Note that not all buttons have the same problem. I have some buttons that work fine while I am using the same code everywhere.

java --module-path "C:\Program Files\javaFX\javafx-sdk-11.0.2\lib" --add-modules javafx.controls --add-modules javafx.graphics --add-modules javafx.fxml -jar demoCinema.jar
        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(AccessController.java:391)
            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:832)
    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:64)
            at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
            at java.base/java.lang.reflect.Method.invoke(Method.java:564)
            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:564)
            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.NullPointerException: Location is required.
            at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3230)
            at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3194)
            at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3163)
            at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3136)
            at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3113)
            at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3106)
            at sample.Controller.reserverenAction(Controller.java:52)
            ... 58 more

Here below is my code for switching from scene

public void reserverenAction(ActionEvent actionEvent) throws IOException {
        Parent naarAdmin = FXMLLoader.load(getClass().getResource("klantPagina.fxml"));
        Scene adminScene = new Scene(naarAdmin);
        Stage window = (Stage) ((Node)actionEvent.getSource()).getScene().getWindow();
        window.setScene(adminScene);
        window.show();
    }

and here below is my main class

package sample;

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

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.getIcons().add(new Image("logoKinepolis/KINE_CMYK.png"));
        primaryStage.setTitle("Cinema");
        primaryStage.setScene(new Scene(root));
        primaryStage.setResizable(false);
        primaryStage.show();
    }


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

I hope someone can help me because it really sucks. I also tried to use another javafx sdk version but that doesn't help. Thanks in advance

  • 1
    if you scroll to the bottom you'll see the origin: `Caused by: java.lang.NullPointerException: Location is required.` Which was caused by your class `at sample.Controller.reserverenAction(Controller.java:52)`, So go to line 52 in your code, in which might be the reserveren code sample you posted, and google that error. You'll find https://stackoverflow.com/questions/20507591/javafx-location-is-required-even-though-it-is-in-the-same-package as the first result – Tschallacka May 21 '21 at 07:08
  • 1
    I am guessing that `reserverenAction(Controller.java:52)` is this line: `Parent naarAdmin = FXMLLoader.load(getClass().getResource("klantPagina.fxml"))`. Have you bundled `"klantPagina.fxml"` in the JAR (please actually open the jar and see if it is there). Under what path? Can you try the full path, e.g. `"/sample/klantPagina.fxml"`? (note that `"/sample"` is the location of the FXML file in the JAR folder structure) – Nikos Paraskevopoulos May 21 '21 at 07:13
  • @NikosParaskevopoulos Thank you very much! It's just because in inteliJ it's not case sensitive while in jar it is. Thank you 10 000 times! – Ilias van den borre May 21 '21 at 08:47
  • 1
    @Tschallacka I love you too! – Ilias van den borre May 21 '21 at 08:47

1 Answers1

0

The resource /sample/klantPagina.fxml might not have been found and give a NullPointerException on load.

    Parent naarAdmin = FXMLLoader.load(getClass().getResource("klantPagina.fxml"));

The package of getClass() determines the directory, sample probably.

So look in the jar (zip format) or on the class path for the resource.

It must be case-sensitive. On Windows sometimes the extension is blended out, and one might actually have a file klantPagina.fxml.fxml but unlikely here.

You might try an absolute path:

    Parent naarAdmin = FXMLLoader.load(Main.class.getResource("/klantPagina.fxml"));
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138