JDK-8175888 : NPE when GTKStyle.getInsets calls getName on the JPanel while in ctor
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 8,9
  • Priority: P3
  • Status: Open
  • Resolution: Unresolved
  • OS: generic
  • CPU: x86_64
  • Submitted: 2017-02-26
  • Updated: 2025-08-18
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.
Other
tbdUnresolved
Description
FULL PRODUCT VERSION :
java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)


ADDITIONAL OS VERSION INFORMATION :
Linux Tichy 4.8.0-39-generic #42~16.04.1-Ubuntu SMP Mon Feb 20 15:06:07 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux


A DESCRIPTION OF THE PROBLEM :
When a subclass of JPanel gets constructed the GTK platform look and feel will call back into the subclasses getName method. When that method gets called the fields of the subclass are not initialized yet. Any operation on those fields will cause a NullPointerException.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Write a subclass of JPanel which operates on a field, that will get initialized on construction.
Set the UIManagers look-and-feel to GTK or Nimbus.
Construct the subclass.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Program exits without an error
ACTUAL -
Nimbus has problems
java.lang.NullPointerException
	at JPanelCtorBug$MyPanel.getName(JPanelCtorBug.java:15)
	at javax.swing.plaf.nimbus.NimbusDefaults$LazyStyle.matches(NimbusDefaults.java:1479)
	at javax.swing.plaf.nimbus.NimbusDefaults$LazyStyle.matches(NimbusDefaults.java:1471)
	at javax.swing.plaf.nimbus.NimbusDefaults.getStyle(NimbusDefaults.java:1160)
	at javax.swing.plaf.nimbus.NimbusLookAndFeel$1.getStyle(NimbusLookAndFeel.java:111)
	at javax.swing.plaf.synth.SynthLookAndFeel.getStyle(SynthLookAndFeel.java:235)
	at javax.swing.plaf.synth.SynthLookAndFeel.updateStyle(SynthLookAndFeel.java:256)
	at javax.swing.plaf.synth.SynthPanelUI.updateStyle(SynthPanelUI.java:117)
	at javax.swing.plaf.synth.SynthPanelUI.installDefaults(SynthPanelUI.java:100)
	at javax.swing.plaf.basic.BasicPanelUI.installUI(BasicPanelUI.java:56)
	at javax.swing.plaf.synth.SynthPanelUI.installUI(SynthPanelUI.java:62)
	at javax.swing.JComponent.setUI(JComponent.java:666)
	at javax.swing.JPanel.setUI(JPanel.java:153)
	at javax.swing.JPanel.updateUI(JPanel.java:126)
	at javax.swing.JPanel.<init>(JPanel.java:86)
	at javax.swing.JPanel.<init>(JPanel.java:109)
	at javax.swing.JPanel.<init>(JPanel.java:117)
	at JPanelCtorBug$MyPanel.<init>(JPanelCtorBug.java:8)
	at JPanelCtorBug$MyPanel.<init>(JPanelCtorBug.java:8)
	at JPanelCtorBug.main(JPanelCtorBug.java:25)
GTK+ has problems
java.lang.NullPointerException
	at JPanelCtorBug$MyPanel.getName(JPanelCtorBug.java:15)
	at com.sun.java.swing.plaf.gtk.GTKStyle.getInsets(GTKStyle.java:316)
	at javax.swing.plaf.synth.SynthStyle.installDefaults(SynthStyle.java:913)
	at javax.swing.plaf.synth.SynthLookAndFeel.updateStyle(SynthLookAndFeel.java:265)
	at javax.swing.plaf.synth.SynthPanelUI.updateStyle(SynthPanelUI.java:117)
	at javax.swing.plaf.synth.SynthPanelUI.installDefaults(SynthPanelUI.java:100)
	at javax.swing.plaf.basic.BasicPanelUI.installUI(BasicPanelUI.java:56)
	at javax.swing.plaf.synth.SynthPanelUI.installUI(SynthPanelUI.java:62)
	at javax.swing.JComponent.setUI(JComponent.java:666)
	at javax.swing.JPanel.setUI(JPanel.java:153)
	at javax.swing.JPanel.updateUI(JPanel.java:126)
	at javax.swing.JPanel.<init>(JPanel.java:86)
	at javax.swing.JPanel.<init>(JPanel.java:109)
	at javax.swing.JPanel.<init>(JPanel.java:117)
	at JPanelCtorBug$MyPanel.<init>(JPanelCtorBug.java:8)
	at JPanelCtorBug$MyPanel.<init>(JPanelCtorBug.java:8)
	at JPanelCtorBug.main(JPanelCtorBug.java:25)


ERROR MESSAGES/STACK TRACES THAT OCCUR :

Nimbus has problems
java.lang.NullPointerException
	at JPanelCtorBug$MyPanel.getName(JPanelCtorBug.java:15)
	at javax.swing.plaf.nimbus.NimbusDefaults$LazyStyle.matches(NimbusDefaults.java:1479)
	at javax.swing.plaf.nimbus.NimbusDefaults$LazyStyle.matches(NimbusDefaults.java:1471)
	at javax.swing.plaf.nimbus.NimbusDefaults.getStyle(NimbusDefaults.java:1160)
	at javax.swing.plaf.nimbus.NimbusLookAndFeel$1.getStyle(NimbusLookAndFeel.java:111)
	at javax.swing.plaf.synth.SynthLookAndFeel.getStyle(SynthLookAndFeel.java:235)
	at javax.swing.plaf.synth.SynthLookAndFeel.updateStyle(SynthLookAndFeel.java:256)
	at javax.swing.plaf.synth.SynthPanelUI.updateStyle(SynthPanelUI.java:117)
	at javax.swing.plaf.synth.SynthPanelUI.installDefaults(SynthPanelUI.java:100)
	at javax.swing.plaf.basic.BasicPanelUI.installUI(BasicPanelUI.java:56)
	at javax.swing.plaf.synth.SynthPanelUI.installUI(SynthPanelUI.java:62)
	at javax.swing.JComponent.setUI(JComponent.java:666)
	at javax.swing.JPanel.setUI(JPanel.java:153)
	at javax.swing.JPanel.updateUI(JPanel.java:126)
	at javax.swing.JPanel.<init>(JPanel.java:86)
	at javax.swing.JPanel.<init>(JPanel.java:109)
	at javax.swing.JPanel.<init>(JPanel.java:117)
	at JPanelCtorBug$MyPanel.<init>(JPanelCtorBug.java:8)
	at JPanelCtorBug$MyPanel.<init>(JPanelCtorBug.java:8)
	at JPanelCtorBug.main(JPanelCtorBug.java:25)
GTK+ has problems
java.lang.NullPointerException
	at JPanelCtorBug$MyPanel.getName(JPanelCtorBug.java:15)
	at com.sun.java.swing.plaf.gtk.GTKStyle.getInsets(GTKStyle.java:316)
	at javax.swing.plaf.synth.SynthStyle.installDefaults(SynthStyle.java:913)
	at javax.swing.plaf.synth.SynthLookAndFeel.updateStyle(SynthLookAndFeel.java:265)
	at javax.swing.plaf.synth.SynthPanelUI.updateStyle(SynthPanelUI.java:117)
	at javax.swing.plaf.synth.SynthPanelUI.installDefaults(SynthPanelUI.java:100)
	at javax.swing.plaf.basic.BasicPanelUI.installUI(BasicPanelUI.java:56)
	at javax.swing.plaf.synth.SynthPanelUI.installUI(SynthPanelUI.java:62)
	at javax.swing.JComponent.setUI(JComponent.java:666)
	at javax.swing.JPanel.setUI(JPanel.java:153)
	at javax.swing.JPanel.updateUI(JPanel.java:126)
	at javax.swing.JPanel.<init>(JPanel.java:86)
	at javax.swing.JPanel.<init>(JPanel.java:109)
	at javax.swing.JPanel.<init>(JPanel.java:117)
	at JPanelCtorBug$MyPanel.<init>(JPanelCtorBug.java:8)
	at JPanelCtorBug$MyPanel.<init>(JPanelCtorBug.java:8)
	at JPanelCtorBug.main(JPanelCtorBug.java:25)


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.UnsupportedLookAndFeelException;

public class JPanelCtorBug {

    private static class MyPanel extends JPanel {

        private static final long serialVersionUID = 1L;
        private String name = "Something";

        @Override
        public String getName() {
            return name.toUpperCase();
        }
    }

    public static void main(String[] args) throws ClassNotFoundException, InstantiationException,
            IllegalAccessException, UnsupportedLookAndFeelException, InterruptedException {
        LookAndFeelInfo[] installedLookAndFeels = UIManager.getInstalledLookAndFeels();
        for (LookAndFeelInfo laf : installedLookAndFeels) {
            UIManager.setLookAndFeel(laf.getClassName());
            try {
                new MyPanel(); // this will result in a NPE on Nimbus and GTK
            } catch (NullPointerException e) {
                System.out.println(laf.getName() + " has problems");
                e.printStackTrace();
            }
        }
    }

}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Change getName of the subclass of JPanel to not depend on fields of the subclass.


Comments
Still it is an issue in the JDK. It is because of bad design of Nimbus and GTK l&f or JPanel. These two l&f are trying to invoke partially constructed object's overridable methods. Actually JPanel is setting the UI from the constructor itself and in that flow, GTK and Nimbus l&f are invoking the getName() method of JPanel. If getName() has overridden in the subclass, and try to use its own data members, this kind of issues will come, because overridden subclass data members are not yet initialized. So better to avoid invoking overridable methods from the constructor. I have attached one sample program(Sample.zip) which demonstrate the same use case. Class B extending from class A and overriding the method getName(). From A's constructor it is invoking overridable method getName(). In this case also we will end up with NullPointerException on creation of 'new B()'.
26-07-2017

In Synth and GTK, the name does not even get initialised by then because only getAccessibleName() method can be used. The test case itself is not to be used. for reference please refer to below comment: /** * Gets the accessible name of this object. This should almost never * return java.awt.Component.getName(), as that generally isn't * a localized name, and doesn't have meaning for the user. If the * object is fundamentally a text object (such as a menu item), the * accessible name should be the text of the object (for example, * "save"). * If the object has a tooltip, the tooltip text may also be an * appropriate String to return. * * @return the localized name of the object -- can be null if this * object does not have a name * @see AccessibleContext#setAccessibleName */ So, as such nothing is overridden by the getName() method in JPanel.
12-07-2017

Avik, this bug pending your re-evaluation
11-07-2017

This is not a test bug, it is a product bug. See the bug description " When a subclass of JPanel gets constructed the GTK platform look and feel will call back into the subclasses getName method. When that method gets called the fields of the subclass are not initialized yet. Any operation on those fields will cause a NullPointerException. " Also workaround given by reporter :"Change getName of the subclass of JPanel to not depend on fields of the subclass. " So making the "name" field as static and/or getName() does not depend on fields of the subclass are two workarounds. So still issues exists in this particular scenario as explained in the bug report. Issue is reproducible in : Nimbus look and feel in Mac OSx and Windows GTK look and feel in Linux.
11-07-2017

as a testbug the issue should be still open until test fixed
10-07-2017

Issue is due to private String name = "Something"; not being set to static. Issue in test case.
10-07-2017

Looks like the issue is in Nimbus only.
02-03-2017

There is error in test case and this is the corrected test case.
02-03-2017

There is a mistake in the test case. Please find attached appropriate test case.
02-03-2017

Avik, Please triage it.
28-02-2017

Checked this with JDK 7u80, 8, 8u121, and 9 ea and could reproduce the issue as described (Run attached test case). 7u80: FAIL 8: FAIL 8u121: FAIL 9 ea b158: FAIL >java JPanelCtorBug Nimbus has problems java.lang.NullPointerException at JPanelCtorBug$MyPanel.getName(JPanelCtorBug.java:15)
27-02-2017