JDK-8032788 : ImageIcon constructor throws an NPE and hangs when passed a null String parameter
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 6u75,7u25,9
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2014-01-27
  • Updated: 2014-10-15
  • Resolved: 2014-05-30
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 7 JDK 8 JDK 9
7u71Fixed 8u20Fixed 9 b22Fixed
Description
javax.swing.ImageIcon constructor doesn't handle null input gracefully.
Instead, a NullPointerException is thrown, and execution hangs.

For example, with 7u25:

Uncaught error fetching image:
java.lang.NullPointerException
        at java.io.FileInputStream.<init>(FileInputStream.java:134)
        at java.io.FileInputStream.<init>(FileInputStream.java:97)
        at sun.awt.image.FileImageSource.getDecoder(FileImageSource.java:53)
        at
sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:263)
        at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:205)
        at sun.awt.image.ImageFetcher.run(ImageFetcher.java:169)

Taking a thread dump then reveals that the main thread has gone into an
Object.wait(), and it will remain there forever because the "Image Fetcher 0"
thread failed to fetch an image:

"Image Fetcher 0" daemon prio=8 tid=0x046e4000 nid=0x7964 in Object.wait()
[0x0520f000]
   java.lang.Thread.State: TIMED_WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x0f0b06e0> (a java.util.Vector)
        at sun.awt.image.ImageFetcher.nextImage(ImageFetcher.java:147)
        - locked <0x0f0b06e0> (a java.util.Vector)
        at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:200)
        at sun.awt.image.ImageFetcher.run(ImageFetcher.java:169)

"main" prio=6 tid=0x0223bc00 nid=0x8784 in Object.wait() [0x0238f000]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x0f0afe38> (a java.awt.MediaTracker)
        at java.awt.MediaTracker.waitForID(MediaTracker.java:667)
        - locked <0x0f0afe38> (a java.awt.MediaTracker)
        at javax.swing.ImageIcon.loadImage(ImageIcon.java:302)
        - locked <0x0f0afe38> (a java.awt.MediaTracker)
        at javax.swing.ImageIcon.<init>(ImageIcon.java:153)
        at javax.swing.ImageIcon.<init>(ImageIcon.java:174)
        at TestImageIcon.main(TestImageIcon.java:3) 
TestCase:

public class TestImageIcon {
    public static void main (String args[]) throws Exception{
        javax.swing.ImageIcon ic = new javax.swing.ImageIcon((String)null);
    }
}

Comments
SQE OK to take this simple fix to CPU14_04
18-06-2014

Hi Sean, It seems you have a different case. In this issue NPE is thrown on other Thread, while the main thread remains hanging.
28-05-2014

Fix: http://cr.openjdk.java.net/~anashaty/8032788/9/webrev.00/
22-05-2014

in accordance with bugDB assignment
27-01-2014