United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-6547087 JPopupMenu.Separator getPreferredSize throws NullPointerException
JDK-6547087 : JPopupMenu.Separator getPreferredSize throws NullPointerException

Details
Type:
Bug
Submit Date:
2007-04-17
Status:
Closed
Updated Date:
2011-05-18
Project Name:
JDK
Resolved Date:
2011-05-18
Component:
client-libs
OS:
windows_xp
Sub-Component:
javax.swing
CPU:
x86
Priority:
P3
Resolution:
Fixed
Affected Versions:
6
Fixed Versions:

Related Reports
Backport:
Duplicate:
Relates:

Sub Tasks

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

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]
                                     
2007-07-11
EVALUATION

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



Hardware and Software, Engineered to Work Together