问题描述
我已经为我的按钮添加了一个图像,但是当我运行该框架时会抛出这个异常.为什么?请帮助我.
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)
第 138 行:
public ImageIcon (URL location) {
this(location, location.toExternalForm());
}
第 91 行:
jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/yahoo_1.gif"))); // NOI18N
我使用这种糟糕的检查(Peter Lang 推荐)它是:System.out.println(getClass().getResource("/Images/yahoo_1.gif"));它返回null,为什么?请帮帮我.
推荐答案
这意味着,getClass().getResource("/Images/yahoo_1.gif")
返回 null.
JavaDoc 指出如果
找不到资源或调用者没有足够的权限来获取资源.
检查
getResource
是否真的返回null
:System.out.println(getClass().getResource("/Images/yahoo_1.gif"));
确保您的路径正确并且在您的类路径中.
Make sure that your path is correct and that it is in your classpath.
编辑:
我刚刚用 NetBeans 试过了.我创建了以下结构
I just tried it with NetBeans. I created the following structure
Source Packages
Images
yahoo_1.gif
并且您的代码运行良好.这是你的结构吗?
and your code worked fine. Is this your structure?
尝试右键单击您的应用程序并选择
Clean and Build
.
Try to right-click on your application and select
Clean and Build
.
这篇关于为什么我的图标处理代码会抛出 NullPointerException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!