0

I recently made a JavaFX program for fur with images. It worked fine, images were loading, until I added maven to the project. After configuring maven (I just discovered what maven is so I'm not very experienced with it), I launched my program and everything works well except loading the image which returns this error:

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException: Input stream must not be null
    at javafx.scene.image.Image.validateInputStream(Image.java:1128)
    at javafx.scene.image.Image.<init>(Image.java:706)
    at com.test.Main.handle(Main.java:1000)
    at com.test.Main.handle(Main.java:26)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3470)
    at javafx.scene.Scene$ClickGenerator.access$8100(Scene.java:3398)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3766)
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:432)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:410)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
    at com.sun.glass.ui.View.notifyMouse(View.java:937)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$4(WinApplication.java:186)
    at java.lang.Thread.run(Thread.java:748)

Line of the error: img = new ImageView(new Image(this.getClass().getResourceAsStream("img/cake.png")));

My pom.xml contains:

        <resources>
            <resource>
                <directory>src/main/resources/img</directory>
                <includes>
                    <include>**/*</include>
                </includes>
            </resource>
        </resources>
<\build>

My main class is located in src/main/java/com.test/Main.java

I looked on google for hours and tried every possibility in my code. I read multiple threads and nothing worked. Maybe the issue is in my pom.xml but as I said earlier, I don't know what to do with it. I also tried putting the image in a the package com.test/img/bread.png but it still doesn't work :'(.

If anyone could help me I would appreciate it a lot. Thanks for taking time and reading my question, if any information is missing, just ask me.

SkyCrafter
  • 21
  • 3
  • The path is wrong according to your snippet `img = new ImageView(new Image(this.getClass().getResourceAsStream("cake.png")));` – Snix Jun 05 '20 at 16:06
  • @Snix No, it should be `"/cake.png"`. It's not really helpful to fix these kinds of questions one at a time, without explaining the whole process of how resources are found. – James_D Jun 05 '20 at 16:08
  • @James_D It's not really helpful downvoting my answer because i'm trying to fix his code which is not working only because he is passing a wrong argument. Sometime, it's not really helpful to know how to build a bridge if it keeps crashing only because you are forgetting a single brick. But hey, i understand your point too. – Snix Jun 05 '20 at 16:10
  • 1
    @Snix I downvoted your answer because it was incorrect. In addition, you don't explain *why*, which means the question is at best only useful to the OP - it's not useful to future users who have similar problems. Therefore, it's not really contributing to the mission of the site. – James_D Jun 05 '20 at 16:10
  • Yes, let's say it like this. You could make a comment and i'd have fixed that. – Snix Jun 05 '20 at 16:11
  • Ah yes thanks. Looks like I'm dumb. But why would maven affect it so much? (I mean, going from getResourceAsStream("images/cake.png") to getResourceAsStream("/cake.png")) – SkyCrafter Jun 05 '20 at 16:12
  • 1
    Read the linked question, which explains how this all works. Look at the structure of your *build* folder (by default `target/classes`) to see the structure your pom.xml results in. More usually, you would configure `src/main/resources` as a resource folder, in which case the correct resource path would be `/img/cake.png`. – James_D Jun 05 '20 at 16:14

0 Answers0