简体   繁体   English

为什么我的图标处理代码会抛出 NullPointerException?

[英]Why does my icon handling code throw a NullPointerException?

I have added an image for my button,but when I run that frame this exception will be thrown.why?please help me.我已经为我的按钮添加了一个图像,但是当我运行该框架时,会抛出这个异常。为什么?请帮助我。

init:

deps-jar:
compile-single:
run-single:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at javax.swing.ImageIcon.<init>(ImageIcon.java:138)
        at ClientGUI.IdAndPasswordFrame.initComponents(IdAndPasswordFrame.java:91)
        at ClientGUI.IdAndPasswordFrame.<init>(IdAndPasswordFrame.java:22)
        at ClientGUI.IdAndPasswordFrame$4.run(IdAndPasswordFrame.java:200)
        at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
BUILD SUCCESSFUL (total time: 1 second)

line 138:第 138 行:

public ImageIcon (URL location) {
    this(location, location.toExternalForm());
}

line91:第 91 行:

 jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/yahoo_1.gif"))); // NOI18N

I use this poor checking (Peter Lang recommended)which is:System.out.println(getClass().getResource("/Images/yahoo_1.gif"));我使用这种糟糕的检查(Peter Lang 推荐),即:System.out.println(getClass().getResource("/Images/yahoo_1.gif")); and it returns null,why?它返回 null,为什么? please help me.请帮我。

This means, that getClass().getResource("/Images/yahoo_1.gif") returns null . 这意味着, getClass().getResource("/Images/yahoo_1.gif")返回null

JavaDoc states that this happens if JavaDoc声明如果发生这种情况

the resource could not be found or the invoker doesn't have adequate privileges to get the resource. 无法找到资源或调用者没有足够的权限来获取资源。

  1. Check if getResource really returns null : 检查getResource确实返回null
    System.out.println(getClass().getResource("/Images/yahoo_1.gif"));

  2. Make sure that your path is correct and that it is in your classpath. 确保您的路径正确并且它在您的类路径中。

EDIT : 编辑

I just tried it with NetBeans. 我刚刚尝试使用NetBeans。 I created the following structure 我创建了以下结构

Source Packages
  Images
    yahoo_1.gif

and your code worked fine. 你的代码工作正常。 Is this your structure? 这是你的结构吗?

Try to right-click on your application and select Clean and Build . 尝试右键单击您的应用程序,然后选择“ Clean and Build

In order to fix this, the images need to be copied in the bin directory - not in src directory. 为了解决这个问题,需要将映像复制到bin目录中 - 而不是复制到src目录中。

Otherwise you will get null all the time on getClass().getResource("image.png") . 否则,你将在getClass()。getResource(“image.png”)上一直得到null。 The path is not null and you can set it as the above - only if you copy the images that you need inside the binary directory, where .class files for your project are located. 该路径不为null ,您可以将其设置为上述内容 - 仅当您复制二进制目录中所需的图像时,项目的.class文件所在的位置。

This fixed the problem. 这解决了这个问题。 Let me know if I helped in this. 如果我帮助我,请告诉我。

Ioana 伊万娜

It looks like getClass().getResource("/Images/yahoo_1.gif") returns null ie the .gif cannot be found on your classpath. 看起来像getClass().getResource("/Images/yahoo_1.gif")返回null即在类路径中找不到.gif。 (Images versus images maybe?) (图像与图像可能?)

The URL being passed in is null from this line: 传入的URL在此行中为空:

getClass().getResource("/Images/yahoo_1.gif")

From the JDK documentation: 从JDK文档:

[getResource(..) returns] A URL object for reading the resource, or null if the resource could not be found or the invoker doesn't have adequate privileges to get the resource [getResource(..)returns]用于读取资源的URL对象,如果找不到资源或者调用者没有足够的权限来获取资源,则返回null

Maybe you meant ("Images/yahoo_1.gif") - ie relative path not absolute? 也许你的意思是(“Images / yahoo_1.gif”) - 即相对路径不是绝对的?

I had the same problem. 我有同样的问题。 What worked for me was: 对我有用的是:

  1. Look into the jar file or in the bin folder(the one with .class files) and see the path of image. 查看jar文件或bin文件夹(带.class文件的文件夹)并查看图像路径。
  2. List item 项目清单
private class HandlerClass implements ActionListener{
        public void actionperformed(ActionEvent event){
            JOptionPane.showMessageDialog(null, String.format("%s", event.getActionCommand()));
        }

}

After reviewing some things when trying to add an image I was presented with the same problem that usually occurs in project with maven.在尝试添加图像时查看了一些内容后,我遇到了与 maven 项目中通常出现的相同问题。

I found a solution that uses the full path to be able to access the image.我找到了一个使用完整路径来访问图像的解决方案。 Also, create a function that returns an icon with the image and automatically scaled according to the dimensions that are sent to it.此外,创建一个 function,它返回一个带有图像的图标,并根据发送给它的尺寸自动缩放。

Path -> directory where the image is located, width -> width of the icon, heigth-> height of the icon I hope it serves you, this is my first contribution in the community path -> 图片所在目录,width -> icon的宽度,heigth-> icon的高度 希望对你有帮助,这是我在社区的第一次投稿

public Icon getIcon(String ruta, int width, int heigth) {
        Image image = (new ImageIcon(ruta)).getImage().getScaledInstance(width, heigth, 0);
        Icon mIcono = new javax.swing.ImageIcon(image);
        return mIcono;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM