0

我有一个 java 应用程序,我偶尔会得到一个java.lang.NullPointerException. 我有这个应用程序在信息亭工作,当这个错误发生时,它会卡住并导致很多问题。

错误是:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(ImageIcon.java:204)
    at GUI.newCardPanel.backButton1MousePressed(newCardPanel.java:1470)
    at GUI.newCardPanel.access$11000(newCardPanel.java:36)
    at GUI.newCardPanel$64.mousePressed(newCardPanel.java:1091)
    at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:280)
    at java.awt.Component.processMouseEvent(Component.java:6386)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3268)
    at java.awt.Component.processEvent(Component.java:6154)
    at java.awt.Container.processEvent(Container.java:2045)
    at java.awt.Component.dispatchEventImpl(Component.java:4750)
    at java.awt.Container.dispatchEventImpl(Container.java:2103)
    at java.awt.Component.dispatchEvent(Component.java:4576)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4633)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4294)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4227)
    at java.awt.Container.dispatchEventImpl(Container.java:2089)
    at java.awt.Window.dispatchEventImpl(Window.java:2518)
    at java.awt.Component.dispatchEvent(Component.java:4576)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:672)
    at java.awt.EventQueue.access$400(EventQueue.java:96)
    at java.awt.EventQueue$2.run(EventQueue.java:631)
    at java.awt.EventQueue$2.run(EventQueue.java:629)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:116)
    at java.awt.EventQueue$3.run(EventQueue.java:645)
    at java.awt.EventQueue$3.run(EventQueue.java:643)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:642)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)

第 1470 行:

backButton1.setIcon(new ImageIcon(new ImageIcon(getClass().getResource("/imagenes/sing-in-pres.png")).getImage()));


public class newCardPanel extends javax.swing.JPanel  { //This one is the 36

1091:(Netbeans 自动生成的代码)

public void mousePressed(java.awt.event.MouseEvent evt) {
            backButton1MousePressed(evt);   //This one is the 1091
        }

图片在src文件夹中。

4

2 回答 2

2

如果您查看 ImageIcon 的源代码,您将看到:

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

这意味着您正在为 URL 传递一个空值。这意味着您的代码在

getClass().getResource("/imagenes/sing-in-pres.png")

正在返回 null。

从那里调试。

于 2013-03-11T17:43:42.387 回答
1

它只是偶尔发生。

有时可能是同步错误的结果。Swing GUI 对象应该事件分派线程上构建和操作。必须同步对共享数据的访问。SwingWorker是一种在后台加载图像时保持 GUI 响应的方法,例如.

于 2013-03-11T20:35:19.140 回答