2

I am a new programmer and have just started using Eclipse. Previously, I used JGRASP and when I wanted to add an image to a button all I did was to create an Image folder within the class's folder and write:

setIcon(newImageIcon(this.getClass().getResource("/Images/name.png")));

I tried the same thing with Eclipse but I get a main:NullPointerException error.
I already read another post and added a source folder to my project named Images but nothing changed. The following is the code segment I have an issue with:

resetButton.setIcon(newImageIcon(this.getClass().getResource("/Images/helpIcon.png")));

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
PTheCoolGuy
  • 95
  • 1
  • 8
  • 2
    May you please provide the directory structure of the project. Please see if this post, regarding how to [add images to Java Project](http://stackoverflow.com/a/9866659/1057230), helps in the given direction or not. – nIcE cOw Jun 20 '15 at 00:31
  • 1
    Try to create *package* `Images` and move your image there. – Pshemo Jun 20 '15 at 00:31
  • 1
    @nIcEcOw has created a helpful [guide](http://stackoverflow.com/questions/9864267/load-icon-image-exception/9866659#9866659) to [tag:embedded-resource]s, cited [here](http://stackoverflow.com/tags/embedded-resource/info). – trashgod Jun 20 '15 at 00:44

2 Answers2

6

You are on right way. You had added resource folder. This is the additional root for your resources. Here, in this folder (as root) you have to create /Image folder and then put files into it:

enter image description here

Alex
  • 4,457
  • 2
  • 20
  • 59
1

You could also place your resources in the root "ttt" folder [Although this is not as professional a solution as the one Alex suggested] and refer to them by name, without any package details.

jpd
  • 683
  • 5
  • 8