3

I keep getting this error no matter what I do to change it. I am very new to JavaFX and thought I would try out a small program. I am just trying to have a picture show up on the scene but I can't even have the image load up on my program. I placed the images in a package(seaapp.images) that is next to the main package(seaapp)

package seaapp;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class SeaApp extends Application {

@Override
public void start(Stage primaryStage) {
    ImageView image = new ImageView(new Image(SeaApp.class.getResourceAsStream("images/space.png")));
    Pane root = new Pane();
    root.getChildren().add(image);
    Scene scene = new Scene(root, 300, 250);

    primaryStage.setTitle("Hello World!");
    primaryStage.setScene(scene);
    primaryStage.show();
}

public static void main(String[] args) {
    launch(args);
}

}

This gives me this error:

Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$152(LauncherImpl.java:182)
at com.sun.javafx.application.LauncherImpl$$Lambda$50/355629945.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException: Input stream must not be null
at javafx.scene.image.Image.validateInputStream(Image.java:1109)
at javafx.scene.image.Image.<init>(Image.java:694)
at seaapp.SeaApp.start(SeaApp.java:27)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(LauncherImpl.java:863)
at com.sun.javafx.application.LauncherImpl$$Lambda$53/1175241703.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl$$Lambda$46/1685538367.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295)
at com.sun.javafx.application.PlatformImpl$$Lambda$48/435396101.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(PlatformImpl.java:294)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/485815673.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$145(WinApplication.java:101)
at com.sun.glass.ui.win.WinApplication$$Lambda$36/1673605040.run(Unknown Source)
... 1 more
Exception running application seaapp.SeaApp
Java Result: 1

This works fine when I add a URL of a picture as the new Image but when I have a downloaded picture, it gives me this error. My netbeans is fully updated too. I have put links to screenshots of how I set up the files. Am I missing something?

Setup of Projects:

https://drive.google.com/file/d/0B_KZLduvxt26dE5XS1p5blE5VFE/view?usp=sharing

Setup of Files:

https://drive.google.com/file/d/0B_KZLduvxt26Nm1sZWROSlRMeUE/view?usp=sharing

Link to Entire Project:

https://drive.google.com/file/d/0B_KZLduvxt26dXVFYlFhREFGZW8/view?usp=sharing

Kelvin Wong
  • 31
  • 1
  • 4
  • It should work. Can you post a screenshot of that folder structure (including the image and the main class)? – Roland Jun 09 '15 at 03:17
  • I have added the screenshots @Roland – Kelvin Wong Jun 09 '15 at 04:06
  • I'm not familiar with how netbeans manages the resources, I use eclipse. The project structure is differenet to the files structure. However, the 2nd screenshot shows that images is at the root of src. Can you try if using "/images/space.png" works? i. e. starting the path at the root with "/". Otherwise someone with netbeans experience has to step in. – Roland Jun 09 '15 at 04:45
  • No, that made no difference. Thanks trying though. @Roland – Kelvin Wong Jun 09 '15 at 05:00
  • You screenshots are confusing. Both the screenshots show different location of your image. The first one shows the image inside `src/seaapp/images`, where as the second one shows it inside `src/images`. – ItachiUchiha Jun 09 '15 at 05:09
  • Well it was in src/seaapp/images before but I tried it outside but it made no difference either way @ItachiUchiha – Kelvin Wong Jun 09 '15 at 05:18
  • if it is inside `src/seaapp/images`, then your approach should have worked fine. You may, however, try with `new Image(getClass().getResource("/seaapp/images/space.png"))`. – ItachiUchiha Jun 09 '15 at 05:24

4 Answers4

2

Add a forward slash / before images/space.png and your code will work fine (That is, if the path is correct).

ImageView image = new ImageView(new Image(SeaApp.class.getResourceAsStream("/images/space.png")));

EDIT:

Its supposed to work fine.

Screenshot

UrsinusTheStrong
  • 1,239
  • 1
  • 16
  • 33
  • @KelvinWong That's strange. Its supposed to work. I tried it before posting the answer. Check the path; maybe it is incorrect. – UrsinusTheStrong Jun 10 '15 at 06:18
  • I don't know what to say. I am doing the exact same thing that you have and I still getting an InvocationTargetException @SSJ2Gogeta – Kelvin Wong Jun 10 '15 at 19:06
0

Do it this way:

Image image = new Image( getClass().getResource( "images/space.png").toExternalForm());
ImageView image = new ImageView( image);

If that doesn't work, find out which path to look:

System.out.println( "Path: " + getClass().getResource("/").toExternalForm());
System.out.println( "Path: " + getClass().getResource("images").toExternalForm());
...

and check if the image is there.

Roland
  • 18,114
  • 12
  • 62
  • 93
  • I tried it and it doesn't work and when i do: 'System.out.println("Path: " + getClass().getResource("images").toExternalForm());' I get the same error but when I do 'System.out.println("Path: " + getClass().getResource("/").toExternalForm());' it runs and gives me: Path: file:/C:/Users/Kelvin/Documents/NetBeansProjects/seaApp/build/classes/ – Kelvin Wong Jun 09 '15 at 21:36
  • Now check the folder structure in "C:/Users/Kelvin/Documents/NetBeansProjects/seaApp/build/classes/". Where exactly is the space.png in there? – Roland Jun 09 '15 at 23:13
  • it is in a folder called images – Kelvin Wong Jun 10 '15 at 01:03
  • So it's full path is "C:/Users/Kelvin/Documents/NetBeansProjects/seaApp/build/classes/images/space.png"? Then System.out.println( "Path: " + getClass().getResource("/images/space.png").toExternalForm()); must work. If it doesn't, put the entire project in a zip file for download. – Roland Jun 10 '15 at 04:21
  • I added a link to the whole project. – Kelvin Wong Jun 10 '15 at 19:11
0

change following line :

ImageView image = new ImageView(new Image(SeaApp.class.getResourceAsStream("images/space.png")))

to

ImageView image = new ImageView(new Image(SeaApp.class.getResourceAsStream("file:./"+"images/space.png")))

reference : https://stackoverflow.com/a/23878479/6336264

Community
  • 1
  • 1
iliyaz
  • 1
0

Had the same problem with image inside jar wrapped into exe The problem was the first capital letter in the file name. Paths are case-sensitive in wrapper but not when you run it under IDE on Windows.