I have a picture resource in my netbeans project and the getResource(...) method returns null, for every combination. Can you see what I am doing wrong from this screenshot?
Asked
Active
Viewed 4,595 times
0
Tom
- 15,514
- 17
- 42
- 51
user2302244
- 717
- 2
- 10
- 26
-
Have you tried `getResource("/javafxaddress/media/1414459175_Save.png");`. And please try to add the code instead of a screenshot. – Tom Oct 28 '14 at 18:26
-
1The below link will help you to findout your issue.. would you mind trying this? http://stackoverflow.com/questions/3803326/this-getclass-getclassloader-getresource-and-nullpointerexception?answertab=votes#tab-top – vembutech Oct 28 '14 at 18:26
-
1Many thanks, Tom. /javafxaddress/media/...png worked perfectly. – user2302244 Oct 28 '14 at 19:20
2 Answers
3
getClass() returns TreeTestController.class (assuming the method is not called on a subclass), which is in the package javafxaddress.view. getResource() uses a path that is relative to the package of the class on which it is called, unless the path starts with /. So your code looks for the file in the package javafxaddress.view.media, and the file is in fact in the package javafxaddress.media.
So the path should be /javafxaddress/media/1414459175_Save.png.
JB Nizet
- 657,433
- 87
- 1,179
- 1,226
0
See if either of these work:
getResource("/media/1414459175_Save.png")
or
Toolkit.getDefaultToolkit().getImage("media/1414459175_Save.png")
(notice one with and one without a prefixed slash)
Martin
- 1,120
- 10
- 14