1
jLabel5.setIcon(new javax.swing.ImageIcon("./i/login.png"));

I'm trying to reference that image. The path is correct, and the image actually exists. When I use the full path (I.E. "C:/ blah blah" it works, but this doesn't?

The image folder is in the bin folder.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Chase
  • 25
  • 1
  • 6
  • It all depends on the directory which the Java application is started from. We don't know anything about your directory hierarchy. What do you want to achieve? – JB Nizet Jul 27 '13 at 20:32
  • The commented code will work in this [answer](http://stackoverflow.com/a/11372350/1057230), if your directory structure adheres to the one described n the same answer. – nIcE cOw Jul 28 '13 at 05:09

1 Answers1

4
//This will retuns the URL of the image file inside your project
  this.getClass().getResource("/i/login.png");

So, your code will be :

URL imageUrl = this.getClass().getResource("/i/login.png");
jLabel5.setIcon(new javax.swing.ImageIcon(imageUrl));

If the image is outside your current package, start the path with /i/login.png, else, don't need the /.

Azad
  • 5,047
  • 20
  • 38
  • It must be stated that this solution (and a very good, portable one it is) depends on the image being in `this`'s jar file or otherwise on the classpath. – Richard Sitze Jul 27 '13 at 20:36
  • @Chase:Does your directory to the image is right? Check it please. – Azad Jul 27 '13 at 20:40
  • line 160: jLabel5.setIcon(new javax.swing.ImageIcon(imageUrl)); – Chase Jul 27 '13 at 20:43
  • @Chase: That means it can't find the specific *source*, When this happens, it returns null, hence the `NullPointerException`, check the directory again and don't forgot about case *sensitivity* in file names. – Azad Jul 27 '13 at 20:51
  • It's all correct, and lowercase... This doesn't make any sense lol. – Chase Jul 27 '13 at 21:08
  • @Chase: ask another question about that, and post your codes with it maybe it'll be solved ;) – Azad Jul 27 '13 at 21:50