The reason you get the NullPointerException is because for some reason the image file you are trying to specify cannot be found. Therefore, the getResource() method returns zero.
In the beginning, you can read about adding icons to this link: "How to use icons"
One way they suggest is to create a method:
protected ImageIcon createImageIcon(String path, String description) { java.net.URL imgURL = getClass().getResource(path); if (imgURL != null) { return new ImageIcon(imgURL, description); } else { System.err.println("Couldn't find file: " + path); return null; } }
The advantage of using this method, in addition to using the utility method, which you can use several times when you want to add an icon, is that it also shows you an error if the image cannot be installed on the specified path.
I strongly suspect that this is due to what you provided. It would be nice to look at the folder structure. Try passing the path as "project / print.gif"
source share