0

I'll preface this by stating that I know many similar listings have been made but I have not found one that resolves my current issue.

All of the following code is within my controller class:

    @FXML
    private void handleButtonAction(ActionEvent event) {
        Button button = (Button)event.getSource();
        String id = button.getId();
        Stage stage = (Stage)button.getScene().getWindow();
        openWindow("AddPartMenu.fxml", stage);
    }


    public void openWindow(String window, Stage stage){
        try{
            Parent root = FXMLLoader.load(getClass().getResource(window));
            Scene scene = new Scene(root);

            System.out.println("Show scene");
            stage.setScene(scene);
            stage.show();
        }catch(Exception e ){
            System.out.println("Failed to open new window: " + e.getMessage());
        }
    }

When I run my application and click the button listed within the code above I get the following error

Failed to open new window
file:/C:/Users/Tyler/Documents/NetBeansProjects/InventoryManagementUI/dist/run167739486/InventoryManagementUI.jar!/inventorymanagementui/AddPartMenu.fxml

Which seems to indicate that it is trying to find a reference inside of a jar file, however when I open the jar archive the "inventorymanagementui/AddPartMenu.fxml" is in the proper place. What I'm getting from the whole situation is that for some reason when I call getClass().getResource() at runtime from inside the Jar it is assuming that it is two layers below it rather than packaged inside the Jar(check image below to see that it is packaged flatly for now). How do I fix something like this?

Current file structure pictured

Current file structure pictured

EDIT:

Here is an image showing the opened Jar archive that is being called by Netbeans File paths

Here is the full stacktrace as requested by calling e.printStackTrace()

javafx.fxml.LoadException:file:/C:/Users/Tyler/Documents/NetBeansProjects/InventoryManagementUI/dist/run2024353111/InventoryManagementUI.jar!/inventorymanagementui/AddPartMenu.fxml

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at inventorymanagementui.FXMLDocumentController.openWindow(FXMLDocumentController.java:154)
at inventorymanagementui.FXMLDocumentController.handleButtonAction(FXMLDocumentController.java:88)
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)
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:8413)
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:381)
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$354(GlassViewEventHandler.java:417)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
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$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException
at inventorymanagementui.FXMLDocumentController.initialize(FXMLDocumentController.java:133)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
... 67 more
Community
  • 1
  • 1
vividpk21
  • 384
  • 1
  • 11
  • 1
    You may have recognized the console output does not contain much information. That's why you usually do `e.printStackTracke()` instead of constructing a error message yourself. Please modify the `catch` clause accordingly and add the full stacktrace to the question. – fabian Aug 15 '18 at 09:03
  • So the jar really contains the path `/inventorymanagementui/AddPartMenu.fxml`? Not through some build hack under `/AddPartMenu.fxml`? The latter would need `getResource("/AddPartMenu.fxml")`. – Joop Eggen Aug 15 '18 at 09:14
  • Updated the post with requested info and direct support of the claim that my jar is built with the same pathing. It still seems that the issue is that my program can't find the path at runtime and there's the '!' after the jar path (in the error logs) which I'm unsure as to what it's specifying. – vividpk21 Aug 15 '18 at 10:16
  • `Caused by: java.lang.NullPointerException at inventorymanagementui.FXMLDocumentController.initialize(FXMLDocumentController.java:133)` You try to dereference some `null` expression in this line of your controller's `initialize` method which makes loading the fxml fail. This is not an issue of the resource not being unavailable... – fabian Aug 15 '18 at 10:20
  • I guess you were right, the stack trace is a much better diagnostic tool than what I was doing. I appreciate the help, my current model has a single controller class that initializes table columns within the initialize method, however said table columns are not within the view I am switching to so I believe that to be the cause of the problem. – vividpk21 Aug 15 '18 at 10:30

1 Answers1

0

After posting your full stacktrace the issue is clear: you don't have a controller set (or the path is incorrect) for AddPartMenu.fxml

If I put the wrong fx:controller path into an existing project of mine I get the same error:

Caused by: javafx.fxml.LoadException: 
file:/F:/Documents/James-Docs/Code/MADContacts/target/MADContacts-1.1-SNAPSHOT.jar!/fxml/Contacts.fxml:17

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.access$700(FXMLLoader.java:103)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:922)
etc...
  • If you look at the error code and the code I posted it should be pretty obvious what I need fixed. When I load a resource the getClass().getResource(window) fails to get the resource by name because the Jar is not referencing the data within it. – vividpk21 Aug 15 '18 at 07:46
  • The original error you posted was somewhat unclear (i.e. not obvious as it could be caused by a multitude of things). The issue you are having is something that I though of, but wasn't sure about until you posted the full stack trace. It looks like you don't have an assigned controller for AddPartMenu.fxml or there is something wrong with it's path reference. – James Cathcart Aug 16 '18 at 01:28