1

I have got an Java Agent and I'm trying to use an ImageResource in the "JTreeCellRendererOpen" class from the "res" folder to show as image icon.

 ImageIcon icon = new ImageIcon(this.getClass().getResource("/res/ntf.gif"));
    super.setIcon(icon);

I try the different examples in the following NullPointerException when trying to use image resource but I always get an NullPointerException.

Does somebody know how to access the given resource?

enter image description here

Community
  • 1
  • 1
PoisonedYouth
  • 548
  • 3
  • 16

1 Answers1

3

Use getResourceAsStream() instead of getResource():

import javax.swing.ImageIcon;
import javax.imageio.ImageIO;
...
        InputStream stream = this.getClass().getResourceAsStream("/ntf.gif");
        ImageIcon icon= new ImageIcon(ImageIO.read(stream));

Put the resource files in Java Agent into folder "Res" with button "Import / Resource":

enter image description here

You'll see the files in Package Explorer like this then:

enter image description here

Knut Herrmann
  • 30,880
  • 4
  • 31
  • 67