10

An ImageIcon is added to button properties using NetBeans.

    print.setFont(new java.awt.Font("Serif", 0, 14)); 
    print.setIcon(new javax.swing.ImageIcon(getClass().getResource("/project/print.gif"))); 
    print.setMnemonic('P');
    print.setText("Print");
    print.setToolTipText("Print");

And when compiled it shows

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(ImageIcon.java:205)
    at project.Editor.initComponents(Editor.java:296)

What am I doing wrong?

Barett
  • 5,826
  • 6
  • 51
  • 55
VenuMadhava
  • 103
  • 1
  • 1
  • 6

5 Answers5

8

The reason that you get a NullPointerException is because for some reason the image file that you're trying to specify cannot be located. So the getResource() method returns a null.

As a start, you can read about adding icons in this link: "How to Use Icons"

One of the ways that they suggest is by creating a method:

/** Returns an ImageIcon, or null if the path was invalid. */
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 having this method, apart from being a utility method that you can use multiple times whenever you want to add an icon, is that it also shows you the error in case the image could not be located at the path specified.

I strongly suspect that this has to do with the path that you've provided. It would be good to look at the folder structure. Try passing the path as "project/print.gif"

Sujay
  • 6,753
  • 2
  • 30
  • 49
1

The expression getClass().getResource("/project/print.gif") invokes method getClass (inherited indirectly from class Object ) to retrieve a reference to the Class object that represents the "Editor class" (your class) declaration. That reference is then used to invoke Class method getResource, which returns the location of the image as a URL. The ImageIcon constructor uses the URL to locate the image, then loads it into memory. The JVM loads class declarations into memory, using a class loader. The class loader knows where each class it loads is located on disk. Method getResource uses the Class object’s class loader to determine the location of a resource, such as an image file. Therefore, you get a NullPointerException and, the image file must be stored in the same location as the "Editor.class" file. The techniques that you tried to use here enable an application to load image files from locations that are relative to the class file’s location

Because of that, you should move "print.gif" file to "/projectName/bin/packageName" folder and try

print.setIcon(new javax.swing.ImageIcon(getClass().getResource("print.gif")));

instead of

print.setIcon(new javax.swing.ImageIcon(getClass().getResource("/project/print.gif")));

Seyit Bilal
  • 191
  • 2
  • 16
1

SOLUTION: You need to add Images Folder in resources Structure be like this src/main/resources/Images/youricon.jpg Check this image

0

It is due to the reason that the image file is not located in the specified directory. You may have mistyped the name somehow or the name was changed.

0

After much struggling, this worked for me (still Novice)

  1. Create package for images
  2. Import the package in your JFrame(s), Control(s), etc where the images are required

as in the screenshot enter image description here

KMunyede
  • 1
  • 1