JavaFX的Maven项目中,调用`getResourceAsStream()`方法返回了空值。

huangapple 未分类评论50阅读模式
英文:

Javafx maven project getResourceAsStream() returning null

问题

我最近为图像制作了一个使用JavaFX的程序。一切都运行良好,图片能够加载,直到我将Maven添加到项目中为止。在配置了Maven之后(我刚刚了解了什么是Maven,所以对此不是很有经验),我启动了程序,除了加载图像出现了问题。错误信息如下:

	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)
	...

错误行:img = new ImageView(new Image(this.getClass().getResourceAsStream("img/cake.png")));

我的 pom.xml 包含:

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

我的主类位于 src/main/java/com.test/Main.java

我在谷歌上搜索了好几个小时,并尝试了我的代码中的每种可能性。我阅读了多个帖子,但没有任何解决方法。也许问题在于我的 pom.xml,但正如我之前所说,我不知道该怎么做。我还尝试将图像放在包 com.test/img/bread.png 中,但仍然不起作用 :'(。

如果有人能帮助我,我将非常感谢。感谢您抽出时间阅读我的问题,如果有任何缺失的信息,请随时问我。

英文:

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 &quot;JavaFX Application Thread&quot; java.lang.NullPointerException: Input stream must not be null
	at javafx.scene.image.Image.validateInputStream(Image.java:1128)
	at javafx.scene.image.Image.&lt;init&gt;(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(&quot;img/cake.png&quot;)));

My pom.xml contains:

        &lt;resources&gt;
            &lt;resource&gt;
                &lt;directory&gt;src/main/resources/img&lt;/directory&gt;
                &lt;includes&gt;
                    &lt;include&gt;**/*&lt;/include&gt;
                &lt;/includes&gt;
            &lt;/resource&gt;
        &lt;/resources&gt;
&lt;\build&gt;

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.

huangapple
  • 本文由 发表于 2020年6月5日 23:58:43
  • 转载请务必保留本文链接:https://java.coder-hub.com/62219468.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定