Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8175888

NPE when GTKStyle.getInsets calls getName on the JPanel while in ctor

XMLWordPrintable

      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.

        1. JPanelCtorBug.java
          1 kB
        2. JPanelCtorBug.java
          1 kB
        3. Sample.zip
          0.7 kB

            abhiscxk Abhishek Kumar
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated: