2

I'm trying to make a Jframe class that has buttons with icons

 public ImageIcon Flag = new ImageIcon(getClass().getResource("resources/Flag.png"));

Gives me a null pointer exception

"C:\Users\Khalidi\Documents\NetBeansProjects\MineSweeper\resources\Flag.png"

Is the full directory to reach the image I want I created the folder resources to house the images I need Where should i place the images? And what line of code should i be writing?

Thanks in advance

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
DatForis
  • 197
  • 1
  • 7

5 Answers5

4

Try using the package path with the same method. for example, if my image is in org.nisheeth.resources package, it should be written as follows.

public ImageIcon flag = new ImageIcon(Test.class.getResource("/org/nisheeth/resource/Flag.png"));
Nisheeth Shah
  • 600
  • 1
  • 9
  • 22
3

When you're loading your images using getClass().getResource(), the JVM in fact uses your CLASSPATH to load the resource (you should take a close look at Class#getResource(...)).

As a consequence, to have your resource available, you must push it to your project output folder (must be something like target, no ?).

Riduidel
  • 22,052
  • 14
  • 85
  • 185
3

As you asked for conventions: using the Maven build infrastructure, you would have

  • src/main/java - java packages
  • src/main/resources - resources like your images
  • src/main/resources/images/Flag.png - some resource (no convention)

In your created jar / .classes

  • /images/Flag.png

    public ImageIcon Flag = new ImageIcon(getClass().getResource("/images/Flag.png"));
    

Here, Class.getResource() uses a relative path to the package of the class; therefore, using an absolute path as above ("/...") is easiest.

APerson
  • 8,140
  • 8
  • 35
  • 49
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
1

You can simply place a folder resources to the folder with compiled classes and make ImageIcon like this.

public ImageIcon Flag = new ImageIcon("resources/Flag.png");
Petr.M
  • 106
  • 1
  • 2
  • 10
  • 2
    Though this answer will work from the IDE, it will no longer work once the app is packaged into a jar – Paul Samsotha Oct 07 '14 at 13:16
  • Thanks for correcting me, I am still beginner. I only uses it without making jar package. May I ask you, why it wouldn't work, if i pack my app into a jar? – Petr.M Oct 07 '14 at 13:29
  • When you pass a string it to the `ImageIcon`, it will look for a file on the file system, starting at the working directory. In the IDE is the working directory is the Project Root. That's why this will work in the IDE. But once in a jar, it becomes a binary resource, that needs to accessed via URL. In the case of the OP's current file structure, the file may not even get put into the jar, unless you customize the IDE's build configuration. I normally put all my resources in the `src` somewhere and use the `Class.getResource("/path/to/resources/image.png");`... – Paul Samsotha Oct 07 '14 at 13:34
  • Where `path` is directly in the `src`. The default build will put everything in the `src` into the class path. And you can access class path resources by obtaining the URL from `getResource`, as others have pointed out. – Paul Samsotha Oct 07 '14 at 13:36
  • 1
    See [this post](http://stackoverflow.com/a/9866659/2587435) for some good reading – Paul Samsotha Oct 07 '14 at 13:38
  • Thanks for your thorough answer. I am sure, it will save a lot of time not only to me. – Petr.M Oct 07 '14 at 13:48
-1

Create a package say ImgPack and paste all your images to this package and access all the images using

    ImageIcon ii = new ImageIcon(System.getProperty("user.dir")+"\\src\\ImgPack\\<Imagename.extention>");
Kushal Shukla
  • 110
  • 1
  • 7