java 将 ImageIcon 添加到 NetBeans 中的 jbutton 时出现空指针异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13151979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 11:40:20  来源:igfitidea点击:

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

javaswingjbuttonimageicon

提问by VenuMadhava

An ImageIcon is added to button properties using NetBeans.

使用 NetBeans 将 ImageIcon 添加到按钮属性。

    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?

我究竟做错了什么?

采纳答案by Sujay

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.

您收到 NullPointerException 的原因是由于某种原因无法找到您尝试指定的图像文件。所以该getResource()方法返回一个空值。

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"

我强烈怀疑这与您提供的路径有关。最好看看文件夹结构。尝试将路径作为“project/print.gif”传递

回答by Seyit Bilal

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

表达方式 getClass().getResource("/project/print.gif")调用 getClass 方法(间接继承自 Object 类)以检索对代表“编辑器类”(您的类)声明的 Class 对象的引用。然后使用该引用来调用类方法 getResource,该方法将图像的位置作为 URL 返回。ImageIcon 构造函数使用 URL 来定位图像,然后将其加载到内存中。JVM 使用类加载器将类声明加载到内存中。类加载器知道它加载的每个类在磁盘上的位置。方法 getResource 使用 Class 对象的类加载器来确定资源的位置,例如图像文件。因此,您会收到 NullPointerException,并且图像文件必须与“Editor.class”文件存储在同一位置。

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

因此,您应该将“print.gif”文件移动到“/projectName/bin/packageName”文件夹并尝试

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

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

instead of

代替

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

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