1

I got an error while using this code to set an image. Also I put my image in src folder. I tried another ways but nothing worked for me.

Code:

    import javafx.application.Application;
    import javafx.geometry.Pos;
    import javafx.scene.Node;
    import javafx.scene.Scene;
    import javafx.scene.image.Image;
    import javafx.scene.image.ImageView;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;

    public class Clock extends Application{

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

        }

        @Override
        public void start(Stage ps) throws Exception {
            ps.setTitle("Analog Clock");
            ps.show();

            StackPane pane = new StackPane();

            pane.setAlignment(Pos.CENTER);

            ImageView image = new ImageView(new Image(getClass().getResourceAsStream("analog.png")));
                    pane.getChildren().add(image);

            //Image img = new Image("analog.png");

            // ImageView iv1 = new ImageView();
            // iv1.setImage(img);

             Scene scene = new Scene(pane);

below are errors Every time I run the code.

Error:


    Exception in Application start method
    java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(Unknown Source)
        at com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Source)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
    Caused by: java.lang.RuntimeException: Exception in Application start method
        at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$152(Unknown Source)
        at com.sun.javafx.application.LauncherImpl$$Lambda$50/14845382.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)
    Caused by: java.lang.NullPointerException: Input stream must not be null
        at javafx.scene.image.Image.validateInputStream(Unknown Source)
        at javafx.scene.image.Image.<init>(Unknown Source)
        at Clock.start(Clock.java:26)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(Unknown Source)
        at com.sun.javafx.application.LauncherImpl$$Lambda$53/2934105.run(Unknown Source)
        at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(Unknown Source)
        at com.sun.javafx.application.PlatformImpl$$Lambda$45/18503843.run(Unknown Source)
        at com.sun.javafx.application.PlatformImpl.lambda$null$170(Unknown Source)
        at com.sun.javafx.application.PlatformImpl$$Lambda$48/19884472.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(Unknown Source)
        at com.sun.javafx.application.PlatformImpl$$Lambda$47/2180324.run(Unknown Source)
        at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
        at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
        at com.sun.glass.ui.win.WinApplication.lambda$null$145(Unknown Source)
        at com.sun.glass.ui.win.WinApplication$$Lambda$36/3326003.run(Unknown Source)
        ... 1 more
    Exception running application Clock

Help me to solve this issue. thanks

Student
  • 21
  • 1
  • 1
  • 4
  • Is the analog.png file in the same package like the clock class? If not, you need to add a slash in front of it like: `"/analog.png"`. This comes from the [Documentation](http://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#getResourceAsStream-java.lang.String-) of `getResourceAsStream()` method. – aw-think Sep 28 '15 at 16:38
  • Also note, that AFAIK you should close your stream yourself (eg. use a try-with-resources statement). Or in this case you might want use one of the other constructors which support resource paths. – Puce Sep 28 '15 at 16:40

2 Answers2

2
Caused by: java.lang.NullPointerException: Input stream must not be null
...
at Clock.start(Clock.java:26)

You've got a NPE at line 26, which is the following line of code:

ImageView image = new ImageView(new Image(getClass().getResourceAsStream("analog.png")));

The culprit is likely the reference to analog.png. The system is failing to read the file from the location you're providing and getResourceAsStream() is returning a null value.

You can further determine this by breaking the line up as:

InputStream inStream = getClass().getResourceAsStream("analog.png");
Image imageObject = new Image(inStream);
ImageView image = new ImageView(imageObject);  

and confirm which line number is referenced by the new stack trace when you run Clock.

Confirm your file analog.png is accessible or correct the path / location of your image file along the path. The rules for using getResourceAsStream() properly when it is returning null are summarized in this Stack Overflow Question.

Community
  • 1
  • 1
JoshDM
  • 4,939
  • 7
  • 43
  • 72
1

Your InputStream is null. you should know that if your Image file is in your source folder then you should direct it to it. right click on your image and select copy qualified name and use that for example

i have a folder in my src folder-(package) called files and my image file is default_profile_icon.png, then i will use this code

new Image("/files/default_profile_icon.png",160,160,false,true)

the first parameter is what you look at, hope you get it now.

i do not know where your image is located,hence this answer

Elltz
  • 10,730
  • 4
  • 31
  • 59