2

I have an application that is a hybrid of Swing (legacy) and JavaFX components. It worked fine until I tried inserting a Preloader into the mix, now all my JavaFX components that utilize fxml files and the FXMLLoader don't work, because an NPE is thrown as soon as new FXMLLoader() (with or without parameters), or FXMLLoader.load(), is called.

I'm using 64-bit Java 7u67.

The stack trace shows:

java.lang.NullPointerException at javafx.fxml.JavaFXBuilderFactory.(JavaFXBuilderFactory.java:85) at javafx.fxml.JavaFXBuilderFactory.(JavaFXBuilderFactory.java:53) at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2782) at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2771)

Prior to inserting a PreLoader, a initializing class does its work and then calls the main() method of the actual application. It's basically the same thing in the Preloader, where the application's main() method is called under the Preloader's start() method.

I'm stumped as to what's the cause. Any ideas?

Adam Law
  • 542
  • 1
  • 7
  • 21

1 Answers1

2

I had to review the source code, but the ones I found online didn't match the line # reported.

Anyway, going by what I saw, it seems that the default class loader becomes null in the transition I described. What I did to fix the problem is to set the class loader to a working instance e.g.

        try {
           fxmlLoader = new FXMLLoader(getClass().getResource("Sample.fxml"));
        } catch(Exception e) {
           FXMLLoader.setDefaultClassLoader(SampleController.class.getClassLoader());
           fxmlLoader = new FXMLLoader(getClass().getResource("Sample.fxml"));
        }

Of course, one could go straight ahead and set the default class loader off the bat.

Adam Law
  • 542
  • 1
  • 7
  • 21
  • This is happening on my app in a Mac when running inside a jlink-created image, but not when running on a full JVM. Couldn't find a workaround, so just removed FXML from the app. What a joke. – Renato Apr 05 '20 at 18:52