0

Hi
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 "Input stream must not be null" 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?
Thanks

Anthony
  • 135
  • 1
  • 14
  • Does this answer your question? [getResourceAsStream returns null](https://stackoverflow.com/questions/16570523/getresourceasstream-returns-null) –  Sep 17 '20 at 12:47
  • @dratenik: Thanks for the link. I will look into it, however, I just remembered that the "Build Main project" in Netbeans cleans the target directory structure and wipes the image. After the build, I copy the .jpg file to the target/classes/${package} subfolder and select "Run Main Project", it builds the project again, which wipes the target/src/${package} tree then runs the project. I have to move the .jpg file to someplace like c:\temp and hard code the link into main and see what happens. – Anthony Sep 17 '20 at 13:33

1 Answers1

0

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

  1. clean the project, by right click on project, and then clean
  2. right click on netbeans project -> properties
  3. go to build - > compiling
  4. make sure compile on save option is selected
    • if it's selected, uncheck it, ok the window, and start from step 0 again.
  5. applying changes
  6. Now check the project by either project run using F6, or right click on main class, and run file
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Thanks, I installed the nb-javac plugin, had the "compile on save" check, and now it's working. – Anthony Sep 17 '20 at 14:02
  • NetBeans is known to have some weirdo caching issues, but honestly version 12 looks very stable. That bullet, might force the ide to perform a clean setup workground. You can download that `nb-javac` plugin from menu `Tools` -> `Plugins`, or perform an ide update –  Sep 17 '20 at 14:03
  • Also accessing the file from `C:/` drive may result some security/permission exceptions, so this could be reason you still find it not working. Try somewhere else out of `C` drive. @Anthony –  Sep 17 '20 at 14:04
  • You're right about accessing "c:/Temp". It did not work, although it did not throw any security exception, only the original null stream error was in the stack trace. I surmise it doesn't like the hardcoded full path "c://temp//image.jpg", not sure why? – Anthony Sep 17 '20 at 17:05
  • I think those `//` should be `\\`, or simply use linux like folder splitting, which java will convert it, so `c:/temp/image.jpg` should work. @Anthony –  Sep 17 '20 at 18:02
  • I have tried all those combinations earlier and alas none are working, simply because the ProgressSilder.class.getResourceAsStream("c:/temp/image.jpg") automatically prepends the class's package name to the resource pathname of "c:/temp/image.jpg", hence the full path to the resource becomes "org\openjfx\mypackage\c:\temp\imag.jpg" and that leads to an invalid path exception. – Anthony Sep 17 '20 at 18:22
  • Oh no! you may not access a resource out of package using `getResourceAsStream()` method! Simply use `new FileInputStream("c:/temp/image.jpg")`. Use `getResourceAsStream()` only when you like to load something embedded(next to a class) @Anthony –  Sep 17 '20 at 18:58