0

I'm trying to set the ImageIcon for a java application, but when I try to obtain the resource I receive a NullPointerException.

This is the directory structure inside of Netbeans:

  • project-name
    • resources -images icon.png
    • src
    • target

Here is the code I'm using to set the ImageIcon:

protected ImageIcon createImageIcon(String path, String description) {
    java.net.URL imgURL = getClass().getResource(path);
    if (imgURL != null) {
        return new ImageIcon(imgURL, description);
    } else {
        LOG.error("Couldn't find file: " + path);
        return null;
    }
}

I've inserted the following custom code for the iconImage property in Netbeans:

Form.setIconImage(createImageIcon("/project-name/resources/images/icon.png", "Icon Description").getImage());

Any help is greatly appreciated.

Oscar
  • 31
  • 3
  • Have u set `Resources` as Resources folder in NetBeans? – Thomas Banderas Jun 28 '18 at 17:32
  • Not sure why the directory structure came out that way but there is an "images" folder nested inside the resources directory. I have also tried cleaning and rebuilding the project. – Oscar Jun 28 '18 at 17:33
  • @ThomasBanderas I have not specifically set Resources as a Resources folder. How do I do that? Thanks. Also, I have tried placing the resources folder inside the src folder. – Oscar Jun 28 '18 at 17:34
  • This question was originally closed as a duplicate of : https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it because someone saw the words "NullPointerException" and assumed it was a duplicate. I reopened the question because the question is about "how to read a resource file" when using Netbeans. – camickr Jun 29 '18 at 00:56
  • `Form.setIconImage(createImageIcon("/icon.png", "Icon Description").getImage());` – John Joe Jun 29 '18 at 01:44

0 Answers0