2

So I am trying to use the library called PreferencesFX, which uses FontAwesomeFX which throws the following error.

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:567)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
    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:567)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: java.lang.NullPointerException
    at de.jensd.fx.fontawesomefx.commons/de.jensd.fx.glyphs.GlyphsFactory.loadFont(GlyphsFactory.java:64)
    at de.jensd.fx.fontawesomefx.commons/de.jensd.fx.glyphs.GlyphsFactory.<init>(GlyphsFactory.java:38)
    at de.jensd.fx.fontawesomefx.fontawesome/de.jensd.fx.glyphs.fontawesome.utils.FontAwesomeIconFactory.<init>(FontAwesomeIconFactory.java:28)
    at de.jensd.fx.fontawesomefx.fontawesome/de.jensd.fx.glyphs.fontawesome.utils.FontAwesomeIconFactory.get(FontAwesomeIconFactory.java:33)
    at com.dlsc.preferencesfx@11.7.0/com.dlsc.preferencesfx.view.UndoRedoBox.<init>(UndoRedoBox.java:17)
    at com.dlsc.preferencesfx@11.7.0/com.dlsc.preferencesfx.PreferencesFx.init(PreferencesFx.java:71)
    at com.dlsc.preferencesfx@11.7.0/com.dlsc.preferencesfx.PreferencesFx.<init>(PreferencesFx.java:64)
    at com.dlsc.preferencesfx@11.7.0/com.dlsc.preferencesfx.PreferencesFx.<init>(PreferencesFx.java:55)
    at com.dlsc.preferencesfx@11.7.0/com.dlsc.preferencesfx.PreferencesFx.of(PreferencesFx.java:102)
    at my.package.tryThis(Clusters.java:38)
    at my.package.Main.main(Main.java:17)
    ... 11 more
Caused by: java.lang.NullPointerException
    at de.jensd.fx.fontawesomefx.commons/de.jensd.fx.glyphs.GlyphsFactory.loadFont(GlyphsFactory.java:62)
    ... 21 more
Exception running application my.package.Main

I found the cause of the problem to sit at line 62 of the Glyphs Factory class but I have no idea how to fix it.

The method GlyphsFactory.class.getResource(pathToIconFont) returns null, instead of returning the font file. I can see that the method is loading the font file via path /de/jensd/fx/glyphs/fontawesome/fontawesome-webfont.ttf (which seem to be correct). I can see that the font is loaded from package de.jensd.fx.glyphs.fontawesome to package de.jensd.fx.glyphs but both packages are in different jars.

IntelliJ Screenshot

Any ideas why getResource() returns null? I reported it to the developer but it seems the FontAwesomeFX library is abandoned.

I am running this with newest IntelliJ on a Mac OS X Big Sur.

dan1st
  • 12,568
  • 8
  • 34
  • 67
  • 2
    classes being in different jars don't matter (assuming you add both jars at runtime ofcourse), because it all gets merged – Coderino Javarino Dec 19 '20 at 21:30
  • You link to the BitBucket page for the code you want to use. Have you tried cloning, building, and trying out any fixes? If you figure it out you'll have your own-built jar you can use in your project, and you can create a pull request back to the repository. – BeUndead Dec 19 '20 at 21:42
  • @CoderinoJavarino That's true before modules. Now with modules resources can be ["encapsulated"](https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/lang/Module.html#getResourceAsStream(java.lang.String)), and the above stack trace shows the use of modules. – Slaw Dec 19 '20 at 21:43
  • @BeUndead: I'd rebuild the project but I have no idea what causes the `NullPointerException`. The path to the font file seems to be correct. – Sherry Reese Dec 19 '20 at 23:12

1 Answers1

4

I was successful in patching the library (after reading this) by changing the following line.

Font.loadFont(GlyphsFactory.class.getResource(pathToIconFont).openStream(), 10.0D);

To this.

Font.loadFont(GlyphsFactory.class.getResourceAsStream(pathToIconFont), 10.0D);
  • What did you do to patch the library? A monkey patch with `sun.misc.Unsafe`? Or another method? – midrare Jan 12 '21 at 15:25