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

JPopupMenu.Separator getPreferredSize throws NullPointerException

XMLWordPrintable

    • b20
    • x86
    • windows_xp
    • Not verified

        FULL PRODUCT VERSION :
        java version "1.6.0"
        Java(TM) SE Runtime Environment (build 1.6.0-b105)
        Java HotSpot(TM) Client VM (build 1.6.0-b105, mixed mode)


        ADDITIONAL OS VERSION INFORMATION :
        Microsoft Windows XP [Version 5.1.2600]

        A DESCRIPTION OF THE PROBLEM :
        In JDK6, JPopupMenu.Separator uses WindowsPopupMenuSeparatorUI that extends BasicPopupMenuSeparatorUI, which calls c.getFont().getHeight() somewhere down the line without checking for null (it would return half of font height).

        JDK5 doesn't have a Windows LaF specific JPopupMenu.Separator UI--it uses BasicPopupMenuSeparatorUI, whose getPreferredSize always returns Dimension(0, 2).

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        Set L&F to WindowsLookAndFeel, and call JPopupMenu.Separator.getPreferredSize()

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        null
        java.awt.Dimension[width=0,height=2]

        (under Java 5)
        ACTUAL -
        null
        java.lang.NullPointerException
        at sun.font.FontDesignMetrics$MetricsKey.init(FontDesignMetrics.java:199)
        at sun.font.FontDesignMetrics.getMetrics(FontDesignMetrics.java:267)
        at sun.swing.SwingUtilities2.getFontMetrics(SwingUtilities2.java:910)
        at javax.swing.JComponent.getFontMetrics(JComponent.java:1592)
        at com.sun.java.swing.plaf.windows.WindowsPopupMenuSeparatorUI.getPreferredSize(WindowsPopupMenuSeparatorUI.java:41)
        at javax.swing.JComponent.getPreferredSize(JComponent.java:1627)
        at PopupMenuNPE.main(PopupMenuNPE.java:13)


        REPRODUCIBILITY :
        This bug can be reproduced always.

        ---------- BEGIN SOURCE ----------
        import javax.swing.JPopupMenu;
        import javax.swing.UIManager;

        public class PopupMenuNPE {
            public static void main(String[] args) {
                try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                    JPopupMenu.Separator a = new JPopupMenu.Separator();
                    System.out.println(a.getFont());
                    System.out.println(a.getPreferredSize());
                }
                catch( Exception e ) {
                    e.printStackTrace();
                }
            }
        }

        ---------- END SOURCE ----------

        CUSTOMER SUBMITTED WORKAROUND :
        Extends JPopupMenu.Separator and set its UI to BasicPopupMenuSeparatorUI:

            private static final class JfeMenuSeparator extends JPopupMenu.Separator
            {
                private static final javax.swing.plaf.ComponentUI ui_s = new BasicPopupMenuSeparatorUI();
                
                public JfeMenuSeparator()
                {
                    this.setUI(ui_s);
                }
            }

        Release Regression From : 5.0u10
        The above release value was the last known release where this
        bug was not reproducible. Since then there has been a regression.

              idk Igor Kushnirskiy (Inactive)
              ryeung Roger Yeung (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: