Reputation: 546
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?
Upvotes: 1
Views: 696
Reputation: 30960
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":
You'll see the files in Package Explorer like this then:
Upvotes: 3