JDK-8344907 : NullPointerException in Win32ShellFolder2.getSystemIcon when "icon" is null
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 17,23,24
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: windows
  • CPU: generic
  • Submitted: 2024-11-22
  • Updated: 2025-01-20
  • Resolved: 2025-01-09
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 25
25 b06Fixed
Related Reports
Relates :  
Description
ADDITIONAL SYSTEM INFORMATION :
Windows, x64. Bug is present in all recent releases.

A DESCRIPTION OF THE PROBLEM :
There is a missing null check in Win32ShellFolder2.java:1217.

Seeing users crash in the wild with:
> Cannot invoke "java.awt.Image.getWidth(java.awt.image.ImageObserver)" because "icon" is null.

1215:  long hIcon = getSystemIcon(iconType.getIconID()); // <-- Can return null
1216:  Image icon = makeIcon(hIcon); // <-- Has internal null check
1217:  if (LARGE_ICON_SIZE != icon.getWidth(null)) { // <-- NullPointerException -- icon is null

Occurs when Windows is unable to provide a system icon for an `optionPaneIcon` in Win32ShellFolderManager2.java:411. The code assumes an icon will *always* be returned, but the Windows API makes no such guarantee.

getSystemIcon calls (via JNI) LoadIcon (User32.dll). This can return null -- and sometimes does.
https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-loadicona

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Reproduces when Windows is unable to return an icon.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
`static Image getSystemIcon(SystemIcon iconType)` in Win32ShellFolder2.java:1214 should return null rather than throwing a NPE.
ACTUAL -
Stack trace:
java.lang.NullPointerException: Cannot invoke "java.awt.Image.getWidth(java.awt.image.ImageObserver)" because "icon" is null
    at sun.awt.shell.Win32ShellFolder2.getSystemIcon(Win32ShellFolder2.java:1217)
    at sun.awt.shell.Win32ShellFolderManager2.get(Win32ShellFolderManager2.java:411)
    at sun.awt.shell.ShellFolder.get(ShellFolder.java:272)
    at com.sun.java.swing.plaf.windows.WindowsLookAndFeel$LazyWindowsIcon.createValue(WindowsLookAndFeel.java:2100)
    at javax.swing.UIDefaults.getFromHashtable(UIDefaults.java:226)
    at javax.swing.UIDefaults.get(UIDefaults.java:274)
    at javax.swing.MultiUIDefaults.get(MultiUIDefaults.java:83)
    at javax.swing.UIManager.get(UIManager.java:1025)
    at sun.swing.DefaultLookup.get(DefaultLookup.java:99)
    at javax.swing.plaf.basic.BasicOptionPaneUI.getIconForType(BasicOptionPaneUI.java:676)
    at javax.swing.plaf.basic.BasicOptionPaneUI.getIcon(BasicOptionPaneUI.java:647)
    at javax.swing.plaf.basic.BasicOptionPaneUI.createMessageArea(BasicOptionPaneUI.java:361)
    at javax.swing.plaf.basic.BasicOptionPaneUI.installComponents(BasicOptionPaneUI.java:200)
    at javax.swing.plaf.basic.BasicOptionPaneUI.installUI(BasicOptionPaneUI.java:160)
    at javax.swing.JComponent.setUI(JComponent.java:740)
    at javax.swing.JOptionPane.setUI(JOptionPane.java:1858)
    at javax.swing.JOptionPane.updateUI(JOptionPane.java:1880)
    at javax.swing.JOptionPane.<init>(JOptionPane.java:1845)
    at javax.swing.JOptionPane.showOptionDialog(JOptionPane.java:869)

CUSTOMER SUBMITTED WORKAROUND :
There is no workaround

FREQUENCY : often



Comments
Changeset: 9f3c5f95 Branch: master Author: Harshitha Onkar <honkar@openjdk.org> Date: 2025-01-09 18:22:33 +0000 URL: https://git.openjdk.org/jdk/commit/9f3c5f95361c75d2b4d9e083901ce5a0ebf355e0
09-01-2025

Additional information from the submitter: A reproducer is not possible for this issue -- the NPE occurs when the Windows API returns null, which it is allowed to do as part of its API design. See documentation for LoadIconA,https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-loadicona Return value Type: HICON If the function succeeds, the return value is a handle to the newly loaded icon. If the function fails, the return value is NULL. To get extended error information, call GetLastError. In most cases it does not fail and return null, but occasionally it does -- for reasons known only to Microsoft. The code in the JDK which calls this API (Win32ShellFolder2.java) doesn't check the error or check for null.
18-12-2024

Requested a reproducer from the submitter.
18-12-2024

[~tongwan] Can you please ask the submitter to attach the reproducer (test case) for this issue. I see the stack trace but there is no test attached in JBS or in the webbug link. STEPS TO FOLLOW TO REPRODUCE THE PROBLEM : Reproduces when Windows is unable to return an icon - Need more details about this scenario.
17-12-2024

A pull request was submitted for review. Branch: master URL: https://git.openjdk.org/jdk/pull/22776 Date: 2024-12-16 23:49:58 +0000
16-12-2024

JDK-8182043 may cause the issue.
23-11-2024