JDK-6547087 : JPopupMenu.Separator getPreferredSize throws NullPointerException
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 6
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2007-04-17
  • Updated: 2011-05-18
  • Resolved: 2011-05-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.
JDK 6 JDK 7
6u4Fixed 7 b20Fixed
Related Reports
Duplicate :  
Relates :  
Description
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.

Comments
EVALUATION we can have getFont == null as a special case.
17-07-2007

EVALUATION this bug is a regression introduced by the fix for 5016492 [XP L&F: Separator between menu items in native give more gap than java]
11-07-2007