Java 为什么我的图标处理代码会抛出 NullPointerException?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2019914/
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
Why does my icon handling code throw a NullPointerException?
提问by Johanna
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.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")); and it returns null,why? please help me.
我使用这种糟糕的检查(Peter Lang 推荐),它是:System.out.println(getClass().getResource("/Images/yahoo_1.gif")); 它返回null,为什么?请帮我。
采纳答案by Peter Lang
This means, that getClass().getResource("/Images/yahoo_1.gif")
returns null
.
这意味着,getClass().getResource("/Images/yahoo_1.gif")
返回null
.
JavaDocstates that this happens if
JavaDoc指出,如果
the resource could not be found or the invoker doesn't have adequate privileges to get the resource.
找不到资源或调用者没有足够的权限来获取资源。
Check if
getResource
really returnsnull
:System.out.println(getClass().getResource("/Images/yahoo_1.gif"));
Make sure that your path is correct and that it is in your classpath.
检查是否
getResource
真的返回null
:System.out.println(getClass().getResource("/Images/yahoo_1.gif"));
确保您的路径正确并且在您的类路径中。
EDIT:
编辑:
I just tried it with NetBeans. I created the following structure
我刚刚用 NetBeans 试过了。我创建了以下结构
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
。
回答by rsp
It looks like getClass().getResource("/Images/yahoo_1.gif")
returns null
i.e. the .gif cannot be found on your classpath. (Images versus images maybe?)
它看起来像getClass().getResource("/Images/yahoo_1.gif")
返回,null
即在您的类路径中找不到 .gif。(图像与图像可能?)
回答by Paolo
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(..) 返回] 用于读取资源的 URL 对象,如果找不到资源或调用者没有足够的权限来获取资源,则返回 null
Maybe you meant ("Images/yahoo_1.gif") - i.e. relative path not absolute?
也许你的意思是 ("Images/yahoo_1.gif") - 即相对路径不是绝对的?
回答by Ioana Marginean
In order to fix this, the images need to be copied in the bindirectory - not in srcdirectory.
为了解决这个问题,需要将图像复制到bin目录中 - 而不是src目录中。
Otherwise you will get null all the time on getClass().getResource("image.png"). The path is not nulland 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.
否则,您将始终在getClass().getResource("image.png")上获得 null 。路径不为空,您可以将其设置为上述内容 - 仅当您将所需的图像复制到二进制目录中时,您的项目的 .class 文件位于该目录中。
This fixed the problem. Let me know if I helped in this.
这解决了问题。如果我在这方面有所帮助,请告诉我。
Ioana
约阿纳
回答by 0_o
I had the same problem. What worked for me was:
我有同样的问题。对我有用的是:
- Look into the jar file or in the bin folder(the one with .class files) and see the path of image.
- List item
- 查看 jar 文件或 bin 文件夹(带有 .class 文件的文件夹)并查看图像的路径。
- 项目清单
回答by sudheer
private class HandlerClass implements ActionListener{
public void actionperformed(ActionEvent event){
JOptionPane.showMessageDialog(null, String.format("%s", event.getActionCommand()));
}
}