0

I'm trying to declare an Image object, but I get a NullPointerException every time I try to use it. This is my constructor:

final Image systemSecureImage = new Image((getClass().getResource("images/notifications/systemSecure.png")).toString());

When I try to use it in a method, it throws the error. For example...

sample.sampleMethod(image);

Any idea what I'm doing wrong? I've also tried it two other ways:

Image systemSecureImage = new Image("images/notifications/systemSecure.png");
Image systemSecureImage = new Image("file://images/notifications/systemSecure.png");
Mike
  • 517
  • 2
  • 5
  • 21
  • Have a look at [jewelsea](http://stackoverflow.com/users/1155209/jewelsea)'s [first and only question](http://stackoverflow.com/questions/10575410/where-does-javafx-scene-image-imageflower-png-look-for-flower-png) :-) – Georgian Apr 06 '14 at 21:27
  • this issue is solved in jdk 1.8...try latest jdk – Anshul Parashar Apr 07 '14 at 06:38

3 Answers3

1

The Image's constructor needs the external form of the resource path.

final Image systemSecureImage = new Image((getClass().getResource("images/notifications/systemSecure.png")).toExternalForm());
Georgian
  • 8,795
  • 8
  • 46
  • 87
0

Are you sure that the NullPointerException is thrown due to the image?
If the line

sample.sampleMethod(image);

throws that NPE, it is because sample is null, i.e. not initiated. Observe the exception stacktrace carefully and determine exactly at which line the exception occurred.

Uluk Biy
  • 48,655
  • 13
  • 146
  • 153
0

You have to create a new source folder in order for javafx to find your image; this worked for me in javafx 2 as well as in javafx 8.

Say you have that project:

project/src
project/lib
project/img
project/img/image.png

just right click on the img folder in eclipse -> Build Path -> Use as source folder
Your image would then be like: new Image("image.png");

Hope it helps, Laurenz

int lawl is over 9000
  • 977
  • 1
  • 15
  • 25
  • I'm actually using NetBeans. Is there something similar I would have to do with it? – Mike Apr 08 '14 at 01:05
  • I am not using Netbeans, but if I'm not mistaken this should work out for you: Right Click on your Project -> Click 'Properties'-> Then you should see 'Sources' highlighted -> Click 'Add folder...' where it says 'Source package folders' – int lawl is over 9000 Apr 08 '14 at 06:28