0

I have a java app and I get occasionally a java.lang.NullPointerException. I have this app working in kiosks and when this error happen, it get stuck and cause a lot of problems.

The error is:

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)

The line 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: (Auto generated code by Netbeans)

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

The images are in the src folder.

harpun
  • 4,022
  • 1
  • 36
  • 40
  • 3
    You're using an ImageIcon to construct an ImageIcon? Why? Also, please make sure that `new ImageIcon` actually returns an object. You know, just to make sure. Actually, do that check on `getResource` already. Chances are, that it can't find the resource you have it looking for. – Refugnic Eternium Mar 11 '13 at 17:31

2 Answers2

2

If you have a look at the source for ImageIcon you will see:

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

Which means you are passing in a null value for URL. Which means your code at

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

is returning null.

Debug from there.

Java42
  • 7,628
  • 1
  • 32
  • 50
  • I cannot make debug since this problem happen occasionally. I can't find any way to replicate the problem. It just happen sometimes and I have the log of everything – Rodolfo del Valle Mar 11 '13 at 18:43
1

It just happen sometimes.

Sometimes may be the result of faulty synchronization. Swing GUI objects should be constructed and manipulated only on the event dispatch thread. Access to shared data must be synchronized. SwingWorker is one approach to keeping the GUI responsive while loading images in the background, for example.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • I just checked that i can replicate the issue here. The one that happens sometimes is this: http://stackoverflow.com/questions/15348872/nullpointerexception-sometimes – Rodolfo del Valle Mar 11 '13 at 21:20
  • That does not eliminate faulty synchronization as the root cause. Try a different machine or look at the approaches cited [here](http://stackoverflow.com/q/7787998/230513). – trashgod Mar 11 '13 at 22:12