英文:
JavaFX project in Netbeans throws Exception "Input stream must not be null"
问题
<h4>你好</h4>
<br>我正在使用位于 Windows 10 上的 Netbeans 12.0,使用 JDK 14。我正尝试构建一个来自 Oracle 文档的样本 JavaFX 代码,链接如下:
https://docs.oracle.com/javafx/2/ui_controls/slider.htm
在 IDE 内运行应用程序时,我遇到了一个运行时错误:“<b><u>输入流不得为空</u></b>”。问题出现在主类中,当它尝试从 .jpg 文件创建 Image 对象时,会导致空流以及随后的 NullPointer 异常。我在 StackOverflow 上看到了类似的问题,共同点是资源文件的路径不正确(即 png 或 jpg 文件)。为了避免这个问题,每次构建后,我都会手动将 .jpg 文件复制到目标文件夹中的 target/classes/${package} 子文件夹中,其中包含所有构建类,但我仍然遇到了 NullPointer 异常。
以下是从主类中摘录的引发异常的部分(第33行):
final Image image = new Image(ProgressSilder.class.getResourceAsStream("C://Temp//cappuccino.jpg"));
异常堆栈跟踪:
构造函数中的异常
java.lang.reflect.InvocationTargetException
在 java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
在 java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
在 java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
在 java.base/java.lang.reflect.Method.invoke(Method.java:564)
在 javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
在 javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
在 java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
在 java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
在 java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
在 java.base/java.lang.reflect.Method.invoke(Method.java:564)
在 java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
由于:java.lang.RuntimeException: 无法构造 Application 实例:类 org.openjfx.mavenfxprogressslider.ProgressSilder
在 javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:890)
在 javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
在 java.base/java.lang.Thread.run(Thread.java:832)
由于:java.lang.reflect.InvocationTargetException
在 java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
在 java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
在 java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
在 java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
在 java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
在 javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:802)
在 javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
在 javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
在 java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
在 javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
在 javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
在 javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
在 javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
... 1 more
由于:java.lang.NullPointerException: 输入流不得为空
在 javafx.graphics/javafx.scene.image.Image.validateInputStream(Image.java:1135)
在 javafx.graphics/javafx.scene.image.Image.<init>(Image.java:702)
在 org.openjfx.mavenfxprogressslider/org.openjfx.mavenfxprogressslider.ProgressSilder.<init>(ProgressSilder.java:33)
... 14 more
运行应用程序时出现异常 org.openjfx.mavenfxprogressslider.ProgressSilder
命令执行失败。
org.apache.commons.exec.ExecuteException: 进程退出并带有错误:1(退出值:1)
在 org.apache.commons.exec.DefaultExecutor.executeInternal(DefaultExecutor.java:404)
在 org.apache.commons.exec.DefaultExecutor.execute(DefaultExecutor.java:166)
在 org.openjfx.JavaFXBaseMojo.executeCommandLine(JavaFXBaseMojo.java:504)
在 org.openjfx.JavaFXBaseMojo.executeCommandLine(JavaFXBaseMojo.java:394)
在 org.openjfx.JavaFXRunMojo.execute(JavaFXRunMojo.java:100)
在 org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:137)
在 org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:210)
在 org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:156)
在 org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:148)
在 org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:117)
在 org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:81)
在 org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:56)
在 org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
在 org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:305)
在 org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:192)
在 org.apache.maven.DefaultMaven.execute(DefaultMaven.java:105)
在 org.apache.maven.cli.MavenCli.execute(MavenCli.java:957)
在 org.apache.maven.cli.MavenCli.doMain(MavenCli.java:289)
在 org.apache.maven.cli.MavenCli.main(MavenCli.java:193)
在 jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
在 jdk.internal.reflect.NativeMethodAccessorImpl.invoke(N
<details>
<summary>英文:</summary>
<h4>Hi</h4>
<br>I am using Netbeans 12.0 on Windows 10 with JDK 14. I am trying to build a sample JavaFX code from the Oracle doc at:
https://docs.oracle.com/javafx/2/ui_controls/slider.htm
I am getting a runtime error "<b><u>Input stream must not be null</u></b>" when running the application within the IDE. The problem occurs in the main when it tries to create an Image object from a .jpg file that leads to a null stream and consequently a NullPointer exception. I have seen similar issues in the StackOverflow and the common denominator was the incorrect path to the resource file (i.e. the png or jpg files). To avoid this problem, after each build, I manually copy my .jpg to the target/classes/${package} sub-folder where all the build classes reside and I am still getting a NullPointer exception.
Here is the excerpt from the main (line 33) that causes the exception according to the stack trace:
final Image image = new Image(ProgressSilder.class.getResourceAsStream("C://Temp//cappuccino.jpg"));
The exception stack trace:
Exception in Application constructor
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:564)
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:564)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Unable to construct Application instance: class org.openjfx.mavenfxprogressslider.ProgressSilder
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:890)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:832)
Caused by: java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:802)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
... 1 more
Caused by: java.lang.NullPointerException: Input stream must not be null
at javafx.graphics/javafx.scene.image.Image.validateInputStream(Image.java:1135)
at javafx.graphics/javafx.scene.image.Image.<init>(Image.java:702)
at org.openjfx.mavenfxprogressslider/org.openjfx.mavenfxprogressslider.ProgressSilder.<init>(ProgressSilder.java:33)
... 14 more
Exception running application org.openjfx.mavenfxprogressslider.ProgressSilder
Command execution failed.
org.apache.commons.exec.ExecuteException: Process exited with an error: 1 (Exit value: 1)
at org.apache.commons.exec.DefaultExecutor.executeInternal (DefaultExecutor.java:404)
at org.apache.commons.exec.DefaultExecutor.execute (DefaultExecutor.java:166)
at org.openjfx.JavaFXBaseMojo.executeCommandLine (JavaFXBaseMojo.java:504)
at org.openjfx.JavaFXBaseMojo.executeCommandLine (JavaFXBaseMojo.java:394)
at org.openjfx.JavaFXRunMojo.execute (JavaFXRunMojo.java:100)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
at org.apache.maven.cli.MavenCli.execute (MavenCli.java:957)
at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:289)
at org.apache.maven.cli.MavenCli.main (MavenCli.java:193)
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:564)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
org.apache.commons.exec.ExecuteException: Process exited with an error: 1 (Exit value: 1)
at org.apache.commons.exec.DefaultExecutor.executeInternal(DefaultExecutor.java:404)
at org.apache.commons.exec.DefaultExecutor.execute(DefaultExecutor.java:166)
at org.openjfx.JavaFXBaseMojo.executeCommandLine(JavaFXBaseMojo.java:504)
at org.openjfx.JavaFXBaseMojo.executeCommandLine(JavaFXBaseMojo.java:394)
at org.openjfx.JavaFXRunMojo.execute(JavaFXRunMojo.java:100)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:137)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:210)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:156)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:148)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:117)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:81)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:56)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:305)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:192)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:105)
at org.apache.maven.cli.MavenCli.execute(MavenCli.java:957)
at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:289)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:193)
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:564)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:282)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:225)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:406)
at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:347)
Any idea how to circumvent this error?<br>
<b>Thanks</b>
</details>
# 答案1
**得分**: 0
听起来像是你的NetBeans配置有问题。
NetBeans可能会对项目执行完整的构建,或者只是必要的元素。看起来你遇到了第二种情况。
尝试一下:
0. 清理项目,通过右键单击项目,然后选择“clean”。
1. 右键单击NetBeans项目 -> “属性”。
2. 转到“构建” -> “编译”。
3. 确保选择了“保存时编译”选项
* 如果已选择,取消选择它,点击“确定”关闭窗口,然后重新从步骤`0`开始。
4. 应用更改
5. 现在通过使用`F6`运行项目,或者右键单击主类,然后选择“run file”来检查项目。
<details>
<summary>英文:</summary>
Sound like a mis-config for your NetBeans.
NetBeans may perform a complete build over project, or just essential elements. Looks like you've got the second one.
try this
0. clean the project, by right click on project, and then `clean`
1. right click on netbeans project -> `properties`
2. go to `build` - > `compiling`
3. make sure `compile on save` option is selected
* if it's selected, uncheck it, `ok` the window, and start from step `0` again.
4. applying changes
5. Now check the project by either project run using `F6`, or right click on main class, and `run file`
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论