0

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?

Netbeans screen shot

Tom
  • 16,842
  • 17
  • 45
  • 54
user2302244
  • 843
  • 2
  • 11
  • 27
  • 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
  • 1
    The 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 – BDRSuite Oct 28 '14 at 18:26
  • 1
    Many thanks, Tom. /javafxaddress/media/...png worked perfectly. – user2302244 Oct 28 '14 at 19:20

2 Answers2

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
  • 678,734
  • 91
  • 1,224
  • 1,255
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,130
  • 10
  • 14