【发布时间】:2020-10-12 19:24:59
【问题描述】:
当我在 Eclipse 3.6.1 中运行我的代码时,我在以下语句中得到 NullPointerException:
private ImageIcon initialicon = new ImageIcon(gbarsStatusPlusTest.class.getResource("/images/waitingLight.GIF"));
我已经阅读了有类似问题的帖子,但没有找到答案,包括:
- 使用绝对路径
- 使用各种相对路径
- 将 gif 文件放入各种文件夹(src、bin、lib 等)
- 创建 waitingLight.gif->waitingLight.GIF 链接
我当前的图片项目路径是LosOMeter->src->images->waitingLight.GIF。
有什么想法吗?
堆栈跟踪:
Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException
at javax.swing.ImaqeIcon.<init>ImaqeIcon.java:205)
at geolosmeter.gbarsStatusPlusTest.<init>(gbarsStatusPlusTest.java:85)
at geolosmeter.gbarsStatusPlusTest$5.run(gbarsStatusPlusTest.java:2848)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:312)
at java.awt.EventQueue.dispatchEventlmp(EventQueue.java:745)
at java.awt.EventQueue.access$300(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:706)
at java.awt.EventQueue$3.run(EventQueue.java:704)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccesslmpl.dolntersectionPrivilegeProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:715)
at java.awt.EventDispatchThread.pumpOneEventForFilter(EventDispatchThread.java:18)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:133)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:22)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:118)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:110)
at java.awt.run(EventDispatchThread.java:91)
【问题讨论】:
-
尝试删除路径开头的 /。这有帮助吗?相对于 gbarsStatusPlusTest 类,您的图像在哪里?是否在同一个 JAR 文件中?
-
这个问题与eclipse无关,与你的系统路径有关。您应该提供图像文件的绝对路径。
-
我尝试删除路径开头的 / 并使用绝对路径。都没有用。
-
“将 gif 放入各种文件夹(src、bin、lib 等)” 由于您尝试将其作为类路径资源加载,因此它属于您的项目的资源目录。见stackoverflow.com/questions/27934796/…
-
你不必将你的 gif 文件放到随机位置,而是它的包含目录
images应该在类路径上。如果您创建一个jar文件,只需将images添加到jar 中。如果您创建一个war文件,请将images放在WEB-INF/classes下的war内。