1

I am having "tare your hair out" type difficulties while trying to open an fxml file from a controller which is in a different package. the package structure is as follows:

Controller name = LocationController Controller package = src/com/yas/prayertimeconfig/location/java

FXML file name = AvailableAddresses.fxml FXML file package = src/com/yas/prayertimeconfig/availableaddresses/java

The code I am using to open up the fxml file from with in LocationController is as follows:

@FXML void btnFindAddress_Click(ActionEvent event) throws IOException {

        try{            
            Parent root1 = FXMLLoader.load(getClass().getResource("/src/com/yas/prayertimeconfig/availableaddresses/java/AvailableAddresses.fxml"));
            Stage stage = new Stage();
            stage.setTitle("Available Addresses");
            stage.setScene(new Scene(root1));
            stage.show();
        } catch (Exception e) {
            System.out.println(e);
        }


    }

I keep getting:

java.lang.NullPointerException: Location is required.

I have tried every using:

getClass().getResource()

and

getClass().getClassLoader().getResource()

and no joy.

Please help!

Yastafari
  • 81
  • 6
  • try using `../availableaddresses/java/AvailableAddresses.fxml`. – fuggerjaki61 Jan 09 '20 at 16:35
  • Does this answer your question? [Java - class.getResource returns null](https://stackoverflow.com/questions/4301329/java-class-getresource-returns-null) – fuggerjaki61 Jan 09 '20 at 16:35
  • @fuggerjaki61 The `getResource` API does not support `..`. It may work when running from the IDE but will fail once packaged in a JAR file. – Slaw Jan 09 '20 at 19:11
  • I assume that your source files are all under a directory named `src`. If that's the case, then you should not include `src` in the resource name. What happens if your try `/com/yas/prayertimeconfig/availableaddresses/java/AvailableAddresses.fxml`? – Slaw Jan 09 '20 at 19:13
  • 1
    @fuggerjaki61 relative paths are not a good idea since navigating "up" only works as long as the app is not deployed as jar. You need to start the path at the classpath root starting with `/` which in this case probably is `/com/yas/prayertimeconfig/availableaddresses/java/AvailableAddresses.fxml` but you can try repeatedly removing everything before the second `/` until it works. (If it doesn't, the resource isn't in the classpath at runtime or the path contains a typo or the case is wrong) – fabian Jan 09 '20 at 19:14
  • [this](https://stackoverflow.com/questions/4301329/java-class-getresource-returns-null/50387930#50387930) might help you – fuggerjaki61 Jan 09 '20 at 19:16
  • Sorry guys, I am a complete dumass. The Controller for the AvailableAddresses.fxml file was not set correctly. something must have glitched with I moved the file from one package to another using "refactor", or I overwrote the setting when I made a change in scenebuilder and saved. Once again, my apologies for the wild goose chase, and I really do appreciate all the of the responses. – Yastafari Jan 10 '20 at 10:31

1 Answers1

0

Here's how I recreated that error, how I troubleshooted it, and how I ultimately fixed it, in that order.

Full Error Message: 

Exception in Application start method
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 javafx.graphics@18.0.1/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:465)
 at javafx.graphics@18.0.1/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:364)
 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 java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
 at javafx.graphics@18.0.1/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:901)
 at javafx.graphics@18.0.1/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:196)
 at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.NullPointerException: Location is required.
 at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3324)
 at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3287)
 at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3255)
 at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3227)
 at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3203)
 at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3196)
 at com.company.Main.start(Main.java:29)
 at javafx.graphics@18.0.1/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:847)
 at javafx.graphics@18.0.1/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
 at javafx.graphics@18.0.1/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
 at java.base/java.security.AccessController.doPrivileged(Native Method)
 at javafx.graphics@18.0.1/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
 at javafx.graphics@18.0.1/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
 at javafx.graphics@18.0.1/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
 at javafx.graphics@18.0.1/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
 ... 1 more

Here's my Start function code that caused the error:

@Override
public void start(Stage stage) throws Exception {
    System.out.println("Start()");
    Parent root = FXMLLoader.load(getClass().getResource("login-form.fxml"));
    stage.setTitle("Login");
    Scene scene = new Scene(root, 200, 150);
    stage.setScene(scene);
    stage.show();
}

The code above basically is trying to load an fxml file on program entry and we can't load it for some reason. (stay tuned)

Basically what that error message is telling me is that it can't load login-form.fxml

For a quick and dirty test, Copy that fxml file that you are trying to load into the same project dir as the the file that has the function that is loading. Just put everything in the same folder. I use copy/paste in IntelliJ's project viewer to do this.

first copy

then paste

Just make sure that your project "sees" the file. You may have to import your fxml project into the file.  It will load now for me and I can see my login form on program launch.

login form shows on program launch

Now, let's fix this the "correct" ie. "less hacky" way. 

As another poster commented, relative paths are not a good idea since navigating "up" only works as long as the app is not deployed as jar. You need to start the path at the classpath root starting with /

java fxml file in different package

You can see in my screencap that I have the fxml file I want to import in a different package called "view". In Java projects a "package" is basically a "folder", so I can use "/view/login-form.xml" as a path I can pass to   FXMLLoader.load(getClass().getResource("/view/login-form.fxml")) Remember that "/" in this case is the root of your project, not the root directory of your entire hard drive.

start function in main class tries to load fxml file in different package

sitting-duck
  • 961
  • 1
  • 10
  • 22