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

JPopupMenu.Separator.getPreferredSize() throws NullPointerException

    XMLWordPrintable

Details

    • Bug
    • Resolution: Duplicate
    • P4
    • None
    • 6
    • client-libs

    Description

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

      ADDITIONAL OS VERSION INFORMATION :
      This has been observed on both Windows XP SP 2 and Windows 2003 SP 1

      EXTRA RELEVANT SYSTEM CONFIGURATION :
      not relevant

      A DESCRIPTION OF THE PROBLEM :
      If getPreferredSize() is called on a JPopupMenu.Separator before it is added to a JMenu, you get a NullPointerException with the following stack trace:

      java.lang.NullPointerException
      at sun.font.FontManager.getFont2D(Native Method)
      at sun.font.FontDesignMetrics.initMatrixAndMetrics(FontDesignMetrics.java:343)
      at sun.font.FontDesignMetrics.<init>(FontDesignMetrics.java:336)
      at sun.font.FontDesignMetrics.getMetrics(FontDesignMetrics.java:284)
      at sun.swing.SwingUtilities2.getFontMetrics(SwingUtilities2.java:915)
      at javax.swing.JComponent.getFontMetrics(JComponent.java:1597)
      at com.sun.java.swing.plaf.windows.WindowsPopupMenuSeparatorUI.getPreferredSize(WindowsPopupMenuSeparatorUI.java:68)
      at javax.swing.JComponent.getPreferredSize(JComponent.java:1632)


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the executable test case.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I was expecting to get a Dimension object back. Even if the object was Dimension (0,0), that would be better than an NPE. Idealling I would get a Dimension object that represented the preferred size using the default menu font.
      ACTUAL -
      See stack trace above.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      See stack trace above.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      package com.silverlink.test;

      import java.awt.*;
      import javax.swing.*;

      public class Test {
          public static void main(String[] args) {
              try {
                  SwingUtilities.invokeAndWait(new Runnable() {
                      public void run() {
                          try {
                              UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                              JPopupMenu.Separator separator = new JPopupMenu.Separator();
                              /*
                               * Uncomment the following to avoid the NPE
                               *
                              JMenu menu = new JMenu();
                              separator.setFont(menu.getFont());
                               *
                               */
                              Dimension size = separator.getPreferredSize();
                              System.out.println("A popup menu's preferred size is " + size);
                          } catch (Exception e) {
                              e.printStackTrace();
                          }
                      }
                  });
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      As shown in the source code above, assigning a font to the Separator avoids the bug. This is counterintuitive, however.

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              igor Igor Nekrestyanov (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: