4

Looked everywhere and still can't find a solution to this problem while using NetBeans.

When I use the following code to load a file by path:

Image owl = new ImageIcon(this.getClass().getResource("/images/owl.gif")).getImage();

I get a NullPointerException. I read somewhere where it suggested creating a new folder and making it a source file for the project, but that didn't help. I've tried multiple suggestions that I found on this site and others, but I'm not getting any results.

I'm starting to wonder if there is something wrong with the way that I am putting the path in, but I'm doing it exactly the way that it shows everywhere else. I've tried every combination of every example that I could find to solve this problem for the last couple of days, but nothing is working.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Jeremy Johnson
  • 469
  • 1
  • 4
  • 17
  • Is `/images/owl.gif` located at the root of the classpath? – Sotirios Delimanolis Aug 15 '13 at 14:18
  • 1
    Please specify how your folder structure is laid out. If this code runs inside a jar then there should be a package (i.e. folder) `images` in it and inside that there should exist `owl.gif` – c.s. Aug 15 '13 at 14:20
  • @SotiriosDelimanolis what do you mean by classpath root? – Jeremy Johnson Aug 15 '13 at 14:20
  • 1
    @Jeremy A Java application is run with a classpath. Everything in the classpath is considered a resource (a class, an image, a properties file, etc.) and can be retrieved by `getResource()`. If a java class is in package `com.company.model` then it will appear in `/com/company/model/...` relative to the root of the classpath. Think of it as a directory structure. – Sotirios Delimanolis Aug 15 '13 at 14:22
  • Check -> build, images, nbproject, src. Inside images is the file specified. – Jeremy Johnson Aug 15 '13 at 14:23
  • Right-Click on images -> Build Path -> use a source folder. – Sotirios Delimanolis Aug 15 '13 at 14:23
  • @SotiriosDelimanolis Right-Clicking doesn't allow for option to change build path, but images has been made a source file for the project – Jeremy Johnson Aug 15 '13 at 14:26
  • @JeremyJohnson : You should've consulted this [answer](http://stackoverflow.com/a/9866659/1057230), which contains a link to [NetBeans Doc](https://netbeans.org/kb/docs/java/gui-image-display.html#package), for that matter :-) though glad you got it sorted. – nIcE cOw Aug 15 '13 at 14:59
  • 1
    @nIcEcOw I kept googling stuff for NetBeans and turned up nothing. Wow. Definitely saving that link to bookmarks if there are any other questions. Thank you. – Jeremy Johnson Aug 15 '13 at 15:20

2 Answers2

3

It seems like the images folder wasn't part of your classpath. In Eclipse, it's not in what they call the Build Path.

Right-click on the images folder, select Build Path and Use as Source Folder. The folder will now be added to the classpath whenever you run your application through Eclipse. If you do this, you need to change your path to

Image owl = new ImageIcon(this.getClass().getResource("/owl.gif")).getImage();

because now everything in images will be put directly on the classpath.

You could instead, make a package called images under your normal src folder and call it as

Image owl = new ImageIcon(this.getClass().getResource("/images/owl.gif")).getImage();
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • Build path is not an option available. The folder images has been included as a source file for the package – Jeremy Johnson Aug 15 '13 at 14:26
  • 2
    OP would also need to change his path to `/owl.gif`. – Ravi K Thapliyal Aug 15 '13 at 14:37
  • 1
    @RaviThapliyal Yeah, true that. – Sotirios Delimanolis Aug 15 '13 at 14:38
  • @RaviThapliyal you may have just fixed it with that little alteration. Let me do a quick double check. I got a successful build after dropping "/images" part off the path name, I'm going to see if I can set the image to the frame real quick. – Jeremy Johnson Aug 15 '13 at 14:40
  • Successful build and was able to change the IconImage for the frame. Thanks guys. Please reflect edit in answer to help others. All other answers right now say to keep the folder name as part of the path, but getting rid of it solves the problem. – Jeremy Johnson Aug 15 '13 at 14:43
  • @JeremyJohnson You're welcome. You can do both, depending on how you setup your source folders and packages. – Sotirios Delimanolis Aug 15 '13 at 14:43
  • Also note that in NetBeans one cannot right click and reveal build path. One must click Run -> Project Configuration -> Customize and from there add the folder as a source under the sources tab. – Jeremy Johnson Aug 15 '13 at 14:44
  • @JeremyJohnson Oh wow, I thought you were on Eclipse. My bad. – Sotirios Delimanolis Aug 15 '13 at 14:44
  • 1
    @SotiriosDelimanolis lol. It's totally listed as NetBeans in the first line of the question, but it's all good. We got it worked out and I thank you for your time. – Jeremy Johnson Aug 15 '13 at 14:45
1

.getResource() returns null when it cannot find the resource. That's where you're getting the null.

Your problem is that the path for owl.gif is incorrect.

Luis Sep
  • 2,384
  • 5
  • 27
  • 33