JDK-8039383 : NPE when changing Windows Theme
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 7u51
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2014-04-07
  • Updated: 2014-07-14
  • Resolved: 2014-06-06
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
7-poolResolved 8-poolResolved 9 b22Fixed
Related Reports
Duplicate :  
Duplicate :  
Relates :  
Relates :  
Relates :  
Relates :  
Description
When changing Windows themes from a theme with visual styles (Windows Aero or Windows Basic) to a classic one, NullPointerException could be thrown from Swing code during component tree validation, or InternalError could be thrown during component painting.

java.lang.NullPointerException
        at com.sun.java.swing.plaf.windows.XPStyle$Skin.getHeight(XPStyle.java:528)
        at com.sun.java.swing.plaf.windows.XPStyle$Skin.getHeight(XPStyle.java:532)
        at com.sun.java.swing.plaf.windows.WindowsLookAndFeel$SkinIcon.getIconHeight(WindowsLookAndFeel.java:2243)
        at com.sun.java.swing.plaf.windows.WindowsTableHeaderUI$XPDefaultRenderer.getTableCellRendererComponent(WindowsTableHeaderUI.java:148)
        at javax.swing.plaf.basic.BasicTableHeaderUI.getHeaderRenderer(BasicTableHeaderUI.java:702)
        at javax.swing.plaf.basic.BasicTableHeaderUI.getHeaderHeight(BasicTableHeaderUI.java:737)
        at javax.swing.plaf.basic.BasicTableHeaderUI.createHeaderSize(BasicTableHeaderUI.java:765)
        at javax.swing.plaf.basic.BasicTableHeaderUI.getPreferredSize(BasicTableHeaderUI.java:796)
        at javax.swing.JComponent.getPreferredSize(JComponent.java:1660)
        at javax.swing.ViewportLayout.layoutContainer(ViewportLayout.java:140)
        at java.awt.Container.layout(Container.java:1503)
        at java.awt.Container.doLayout(Container.java:1492)
        at java.awt.Container.validateTree(Container.java:1688)
        at java.awt.Container.validateTree(Container.java:1697)
        at java.awt.Container.validateTree(Container.java:1697)
        at java.awt.Container.validateTree(Container.java:1697)
        at java.awt.Container.validateTree(Container.java:1697)
        at java.awt.Container.validateTree(Container.java:1697)
        at java.awt.Container.validate(Container.java:1623)
        at java.awt.Window.dispatchEventImpl(Window.java:2717)
        at java.awt.Component.dispatchEvent(Component.java:4687)
        at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
        at java.awt.EventQueue.access$200(EventQueue.java:103)
        at java.awt.EventQueue$3.run(EventQueue.java:694)
        at java.awt.EventQueue$3.run(EventQueue.java:692)
        ...

java.lang.InternalError: HTHEME is null
        at sun.awt.windows.ThemeReader.paintBackground(Native Method)
        at sun.awt.windows.ThemeReader.paintBackground(ThemeReader.java:119)
        at com.sun.java.swing.plaf.windows.XPStyle$SkinPainter.paintToImage(XPStyle.java:679)
        at sun.swing.CachedPainter.paint0(CachedPainter.java:146)
        at sun.swing.CachedPainter.paint(CachedPainter.java:111)
        at com.sun.java.swing.plaf.windows.XPStyle$Skin.paintSkinRaw(XPStyle.java:625)
        at com.sun.java.swing.plaf.windows.AnimationController.paintSkin(AnimationController.java:253)
        at com.sun.java.swing.plaf.windows.XPStyle$Skin.paintSkin(XPStyle.java:604)
        at com.sun.java.swing.plaf.windows.WindowsTableHeaderUI$XPDefaultRenderer.paint(WindowsTableHeaderUI.java:204)
        at javax.swing.CellRendererPane.paintComponent(CellRendererPane.java:151)
        at javax.swing.plaf.basic.BasicTableHeaderUI.paintCell(BasicTableHeaderUI.java:710)
        at javax.swing.plaf.basic.BasicTableHeaderUI.paint(BasicTableHeaderUI.java:652)
        at javax.swing.plaf.ComponentUI.update(ComponentUI.java:161)
        at javax.swing.JComponent.paintComponent(JComponent.java:778)
        at javax.swing.JComponent.paint(JComponent.java:1054)
        at javax.swing.JComponent.paintChildren(JComponent.java:887)
        at javax.swing.JComponent.paint(JComponent.java:1063)
        at javax.swing.JViewport.paint(JViewport.java:731)
        at javax.swing.JComponent.paintChildren(JComponent.java:887)
        at javax.swing.JComponent.paint(JComponent.java:1063)
        at javax.swing.JComponent.paintChildren(JComponent.java:887)
        at javax.swing.JComponent.paint(JComponent.java:1063)
        at javax.swing.JComponent.paintChildren(JComponent.java:887)
        at javax.swing.JComponent.paint(JComponent.java:1063)
        at javax.swing.JLayeredPane.paint(JLayeredPane.java:585)
        at javax.swing.JComponent.paintChildren(JComponent.java:887)
        at javax.swing.JComponent.paintToOffscreen(JComponent.java:5226)
        at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:295)
        at javax.swing.RepaintManager.paint(RepaintManager.java:1249)
        at javax.swing.JComponent.paint(JComponent.java:1040)
        at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:39)
        at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:78)
        at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:115)
        at java.awt.Container.paint(Container.java:1967)
        at java.awt.Window.paint(Window.java:3877)
        at javax.swing.RepaintManager$3.run(RepaintManager.java:819)
        ...
Comments
This bug wasn't fixed��� The push of the initial fix caused build failure, JDK-8046239, then after the build issue was resolved, it caused regression JDK-8046391: Hang displaying JFileChooser with Windows L&F. So the fix was backed out from the repo under JDK-8046391. A new bug JDK-8046559 was submitted to deliver the fix that doesn't cause any problems. To avoid confusion, I closed backports of this issue as duplicates of corresponding backports of JDK-8046559 which delivers the working fix.
09-07-2014

Root cause analysis: Windows theme data are accessed via XPStyle and ThemeReader. When the theme is switched to a classic one, theme handles become unavailable, and theme data cannot be accessed any more. The change in theme is posted to EDT; at the same time, the UI often needs to repaint before the theme change is completely handled in Swing. This leads to NPE and/or InternalError as the code tries to access the data that has become unavailable. Additionally, the UI delegates usually assume XPStyle object either exist or doesn't exist, the case where the change occurs from visual styles painting to classic painting wasn't handled carefully. The fix: Windows desktop properties are updated right on Windows Toolkit thread, and the value of "win.xpstyle.themeActive" is stored in ThemeReader.xpStyleEnabled field. PropertyChangeEvents for desktop properties are delivered either synchronously on EDT or asynchronously via DesktopPropertyChangeSupport, as it used to be. Getters in XPStyle class check for null values and return dummy defaults if ThemeReader returned null. SkinPainters also check whether theming is still available before asking ThemeReader to paint. Access to XPStyle.xp instance is blocked as soon as user switches themes. The object will be cleaned when the corresponding PropertyChangeEvent is handled by Swing.
08-07-2014

Note that all back-ports of the fix should also address JDK-8046239. You can use a commit message with two bug IDs on separate lines if you want to combine both fixes together (which would be the preferred way, since the original fix causes a build failure.)
09-06-2014

A good application to reproduce the issue is SwingSet3 (https://java.net/projects/Swingset3). Change Look-and-Feel to Windows, and click JInternalFrame in the list of components on the left. Create several more internal frames. Try switching Windows themes: Windows 7 Basic ��� Windows Classic.
07-05-2014