gpt4 book ai didi

java - InputStream 不会读取 Runnable JAR 中的图像

转载 作者:行者123 更新时间:2023-12-02 09:59:42 26 4
gpt4 key购买 nike

我正在为 D&D 编写一个角色生成器,但我发现自己处于一种奇怪的情况,因为 InputStream 类似乎适用于某些文件,然后我确实得到:java.lang.NullPointerException:输入流不得为空

我正在使用 InputStream 将 javafx Image 构造函数加载到 ImageView 构造函数中[new ImageView(new Image(getClass().getResourceAsStream(..)))];当我加载一些 .jpeg 图像时它可以工作,但当我对 .png 图像执行相同的过程时它不起作用

我已经尝试了 StackOverflow 上描述的许多解决方案,但似乎没有一个太接近我的问题或根本没有帮助:

  • creating a Source folder and retrieving the images from there
  • moving the image files to the class package
  • reading all the possible documentation

这段代码工作正常,它基本上读取图像并将它们作为某些按钮的背景

ImageView imv;
for(int i = 1; i <= 8; i++) {
imv = new ImageView(new Image(getClass().getResourceAsStream("/ButtonImages/" + i + ".jpeg")));
...

这是构建结果(在 JAR 上工作就像在 Eclipse 上工作一样) build result

然后这段代码,应该加载所有D&D种族图像

this.imv = new ImageView(new Image(getClass().getResourceAsStream("/RaceImages/" + displayname + ".png")));

它实际上确实在 Eclipse 中显示为这样,但在 Runnable jar 上报告了错误 eclipse build

这是我的项目结构的图像。

code structure

这就是我执行 Runnable JAR 时期望发生的情况 expectancy

这就是我得到的:

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

这明确表明我在读取比赛图像时得到一个空的InputStream

注意:我实际上提取了 jar 所在的 RaceImages 文件夹,并仅通过命令行运行它(不是我想要的解决方案),运行应用程序 Ambiguous Method

最佳答案

我通过 try-catch 发现了这个问题,我为没有早点这样做而感到羞愧。

最终,唯一的错误是图片名称中的单个大写字母,Eclipse 绕过了该错误,但在从 .jar 运行时导致了 NullPointerException。

我认为此问题已解决,并且不需要进一步干预

这是显示问题的图片

error

关于java - InputStream 不会读取 Runnable JAR 中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55728445/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com