Null pointer exception when an ImageIcon is added to jbutton in NetBeans
Asked Answered
P

5

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?

Peeler answered 31/10, 2012 at 5:51 Comment(5)
The issue is with getClass().getResource("/project/print.gif")). I bet that the image that you're searching for cannot be found at the location that you're trying to point.Schargel
Print.gif image is located in /project/print.gif and the error is same as above.Peeler
your resource getClass().getResource("/project/print.gif") may be null pl checkLeanoraleant
Steps for checking getClass().getResource("/project/print.gif").Peeler
what is getClass method ? it should be YourClassName.this.getClass()Leanoraleant
S
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"

Schargel answered 31/10, 2012 at 6:20 Comment(1)
How can you change the path when is Netabeans Designer generated code?Langue
E
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")));

Earthstar answered 18/10, 2017 at 15:52 Comment(0)
P
1

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

Pretoria answered 30/5, 2021 at 10:0 Comment(0)
A
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.

Aurelea answered 27/11, 2020 at 17:5 Comment(0)
T
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

Toledo answered 18/11, 2022 at 19:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.