6

I don't understand is how to add a simple image. I imported everything and followed what they said on this page:

http://www.java2s.com/Code/Java/JavaFX/LoadajpgimagewithImageanduseImageViewtodisplay.htm

Javafx code

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class test extends Application {

    @Override
    public void start(Stage stage) {
        stage.setTitle("HTML");
        stage.setWidth(500);
        stage.setHeight(500);
        Scene scene = new Scene(new Group());
        VBox root = new VBox();    

        final ImageView selectedImage = new ImageView();   
        Image image1 = new Image(test.class.getResourceAsStream("C:\\Users\\user\\Desktop\\x.jpg"));

        selectedImage.setImage(image1);

        root.getChildren().addAll(selectedImage);

        scene.setRoot(root);

        stage.setScene(scene);
        stage.show();
    }

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

Error

    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$156(LauncherImpl.java:182)
    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:1110)
    at javafx.scene.image.Image.<init>(Image.java:694)
    at prototype.test.start(test.java:23)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(PlatformImpl.java:294)
    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$149(WinApplication.java:191)
Gautam Savaliya
  • 1,403
  • 2
  • 20
  • 31
Asperger
  • 3,064
  • 8
  • 52
  • 100
  • 1
    `test.class.getResourceAsStream("C:\\Users\\user\\Desktop\\x.jpg")` returns `null`, did you try using `/` as the documentation specifies? – BeyelerStudios May 09 '16 at 09:08

6 Answers6

7

Class.getResourceAsStream tries to load an element in the classpath. It's not meant to load files unless they are included in the classpath. To load a file outside of the classpath, use a FileInputStream instead:

Image image1 = new Image(new FileInputStream("C:\\Users\\user\\Desktop\\x.jpg"));

Or use the Image(String) constructor and pass the URL of the file:

Image image1 = new Image(new File("C:\\Users\\user\\Desktop\\x.jpg").toURI().toURL().toExternalForm());
fabian
  • 80,457
  • 12
  • 86
  • 114
2

As fabian mentioned, Class.getResourceAsStream(...) loads an InputStream from the classpath, (meaning, from the jar file itself). To load a file from your computer, you can simply use the file protocol and give the path to the Image constructor, like so:

Image image1 = new Image("file:///C:/Users/user/Desktop/x.jpg");

The reason that you were getting:

Caused by: java.lang.NullPointerException: Input stream must not be null

in your error is because Class.getResourceAsStream(...) simply returns null when it can't find the resource that you tell it to look for. The Image constructor was receiving null and then throwing an exception.

Kröw
  • 504
  • 2
  • 13
  • 31
1

Try adding image to GridPane

ImageView img1 = new ImageView(new 
Image(getClass().getResourceAsStream("C:\\Users\\user\\Desktop\\x.jpg")));
    GridPane.setConstraints(img1, 0, 4);
Heri
  • 4,368
  • 1
  • 31
  • 51
Charitha.R
  • 13
  • 7
0

Your problem seems to be this line:

Image image1 = new Image(test.class.getResourceAsStream("C:\\Users\\user\\Desktop\\x.jpg"));

Because you get a NullPointerException: java.lang.NullPointerException: Input stream must not be null.

See:

Community
  • 1
  • 1
pzaenger
  • 11,381
  • 3
  • 45
  • 46
0

I think a good way to fix this is dragging your image to the project folder. That way you can search for the name alone

Image image1 = new Image(test.class.getResourceAsStream("x.jpg"));

if this does'nt work add File:

-1

Your problem is at line getResourceAsStream("C:\\Users\\user\\Desktop\\x.jpg")); Replace "\\" to "/", your problem is solved

Numb
  • 1
  • Hey, so I know this is an old question, but I just wanted to let you know why someone down-voted this (I might be somewhat wrong myself): the "\\" is used for Windows paths, so this wouldn't have been the problem, unless maybe they were using Mac/Linux. Sorry that your first post was down-voted, but good luck with your future endeavors! – Levi Muniz Jul 05 '18 at 04:41