我正在重建一个我为提高效率而创建的简单游戏程序。当我尝试将图标设置为 JButton 时遇到了麻烦。该程序的更简单版本可以完美运行,在这方面没有任何问题。我在这里使用相同的代码来完成相同的任务,但是我遇到了很多以前从未见过的错误。我可以得到一些帮助来理解错误吗?
这是 .jpg 文件的路径:/Program/src/fire.jpg
这是我用来将图标添加到按钮的代码。错误指向以 开头的行new
,特别是new
当我在 Eclipse 中将它们隔开时的工作。错误不在于hand.[d]
,我已经彻底测试过了。感谢您提供的任何帮助。
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {GraphicGameBoard.hand[d].setIcon(new
javax.swing.ImageIcon(getClass().getResource("/Program/fire.jpg")));}});
堆栈跟踪:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at Program.Main$2.run(Main.java:168)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)