0

This is my first Question so bear with me if I get something wrong but I have a strange issue with the FXMLLoader that I just can't explain myself so any hints or help would be greatly appreciated.

The following snippet is how I load the File GraphicSettings.fxml into my program.

@FXML
public void showGraphicSettings()
{
    System.out.println("Showing graphic settings");
    if (graphicSettings == null)
    {
        try
        {
            FXMLLoader loader = new FXMLLoader();
            System.err.println(getClass().getResource("GraphicSettings.fxml"));
            System.err.println(getClass().getResource("/GraphicSettings.fxml"));
            loader.setLocation(getClass().getResource("GraphicSettings.fxml"));
            System.err.println(loader.getLocation());
            graphicSettings = (GridPane) loader.load();

            GraphicSettingsController graphicSettingsController = loader.getController();
            graphicSettingsController.setSettingsModel(settingsModel);
            graphicSettingsController.setMainPresenter(mainPresenter);
            System.err.println(graphicSettings);
        }
        catch (IOException e)
        {
            System.err.println("GraphicSettings.fxml not found");
            e.printStackTrace();
        }
    }
    mainPresenter.setViewContent(graphicSettings);
}

The output for this snippet is the following:

file:/D:/Gits/TamClipnyera/bin/settings/GraphicSettings.fxml
null
file:/D:/Gits/TamClipnyera/bin/settings/GraphicSettings.fxml
GraphicSettings.fxml not found
javafx.fxml.LoadException: /D:/Gits/TamClipnyera/bin/settings/GraphicSettings.fxml:9

at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2625)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2603)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2435)
at clip/settings.MainSettingsController.showGraphicSettings(MainSettingsController.java:81)
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:1784)
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:54)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3564)
at javafx.graphics/javafx.scene.Scene$ClickGenerator.access$8200(Scene.java:3492)
at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3860)
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)

As I understand it, the File is found in the right place and the URL it gives to the FXMLLoader is correct as well but for some reason, the FXMLLoader cannot find the file.

Now, that in itself would not be as confusing but I am running another two FXMLLoaders with an identical structure for other sub-menu panes (only one is shown at a time) but those work perfectly fine without any issues whatsoever.

So, in essence, I have three fxml Files in the same folder, accessed in the same way yet I am getting different results which is leaving me rather stumped at the potential explanation for this behaviour.

for anyone interested in the full code I am referencing: This is the Controller class in question

GamerWoona
  • 11
  • 2
  • think _package_ when looking up resources: the path to the resource can be either relative to a calling class path (when using class.getResource without leading slash) or absolute from the root of the application path (when using class.getResource with leading slash or always when using classloader.getResource). Typical errors are: project setup such that the resources are not copied over to the bin (output) path, spelling errors, upper/lowerCase errors or similar low-level oversights. – kleopatra Jan 18 '20 at 13:44

2 Answers2

2

Your project structure is a complete mess, I would start by cleaning this up. You should not store your classes and fxmls in the same folder. It should more or less looke like this:

src
|-main
    |-java <-- this is for your classes
        |-application 
        |-controllers

    |-resources <-- this is there your fxmls should go
        |-fxml
        |-style

Then try the following:

GridPane root = (AnchorPane) FXMLLoader.load(getClass().getClassLoader().getResource("fxml/GraphicSetting.fxml"))

Make sure that in your fxml you give qualified name for the controller

wisnix
  • 180
  • 1
  • 8
0

I managed to find a solution to my issue. The path I used was indeed correct and not the culprit. Apparently I had misunderstood or falsely interpreted the stack trace. It was not that the FXMLLoader could not find the file but that I failed to properly populate the ChoiceBoxes inside.

I had done so in the constructor instead of a separate method or the initialize method. Frankly, the snippets I posted made it impossible to find the error, so I apologize for that.

Edit: Also fixed setup

GamerWoona
  • 11
  • 2