I'm writing a character builder for D&D, but I find myself in a weird situation, since InputStream class seems to work for certain files, then I do get: java.lang.NullPointerException: Input stream must not be null
I am using the InputStream to load a javafx Image constructon into an ImageView constructor [new ImageView(new Image(getClass().getResourceAsStream(..)))]; It works when I'm loading some .jpeg images, but it doesn't when I do the same process on .png images
I've tryed many solutions described here on StackOverflow, but none seemed to get too close to my issue or to help at all:
- creating a Source folder and retrieving the images from there
- moving the image files to the class package
- reading all the possible documentation
This piece of code works fine, which basically reads the images and puts them as background for some buttons
ImageView imv;
for(int i = 1; i <= 8; i++) {
imv = new ImageView(new Image(getClass().getResourceAsStream("/ButtonImages/" + i + ".jpeg")));
...
and this is the build result (works on the JAR as it works on Eclipse)
Then this piece of code, should load all the D&D races images
this.imv = new ImageView(new Image(getClass().getResourceAsStream("/RaceImages/" + displayname + ".png")));
And it actually does show uplike this in Eclipse, but reports the error down there on the Runnable jar
This is an image of how my Project is structured.
This is what I am expecting to happen when I execute my Runnable JAR
and this is what I get:
C:\Users\*****\Desktop>java -jar Dnd.jar
Width: 1238.6666666666667 Height: 720.0
Exception in Application start method
Exception in thread "main" java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.base/java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:61)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(Unknown Source)
at java.base/java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException: Input stream must not be null
at javafx.graphics/javafx.scene.image.Image.validateInputStream(Unknown Source)
at javafx.graphics/javafx.scene.image.Image.<init>(Unknown Source)
at dnd.userinterface.javafx.scene.CharactersPane.<init>(CharactersPane.java:55)
at dnd.userinterface.javafx.scene.DndPane.bookPane(DndPane.java:199)
at dnd.userinterface.javafx.scene.DndPane.initPane(DndPane.java:99)
at dnd.userinterface.javafx.scene.DndPane.<init>(DndPane.java:70)
at dnd.userinterface.javafx.application.DNDApplication.start(DNDApplication.java:52)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(Unknown Source)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(Unknown Source)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(Unknown Source)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(Unknown Source)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(Unknown Source)
... 1 more
Which explicitly says that I am getting a null InputStream while reading the race images
Note: I actaully extracted the RaceImages folder where the jar is located, and running it through command line only (not the solution I'd ever want), runs the application
I discovered the issue with a try-catch and I feel ashamed for not doing that earlier.
Eventually, the only mistake was a single capital letter in the name of a picture that was bypassed by Eclipse, but led to NullPointerException when running from .jar.
I deem this as resolved, and no further intervention is needed.
Here's a picture showing the issue