0

The following:

public class TestCode {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                System.err.println("Showing popup");
                // Create a popup menu and fill it with some component
                JPopupMenu basicPopupMenu = new JPopupMenu();
                JTextArea textArea = new JTextArea();
                textArea.setLineWrap(true);
                basicPopupMenu.add(textArea);
                // This is not necessary for the crash, just to verify the pref size is not null at this point
                System.out.println("Popup pref size is : " + basicPopupMenu.getPreferredSize());
                System.out.println("Text area pref size is : " + textArea.getPreferredSize());
                basicPopupMenu.show(null, 0, 0);
                // This is not necessary for the crash, just to verify the minimum size is not null at this point
                System.out.println("Min size is : " + basicPopupMenu.getMinimumSize());
                System.out.println("Text area min size is : " + textArea.getMinimumSize());
                // If I uncomment the next line, then the crash does not occur
                // textArea.getPreferredSize()
                try {
                    basicPopupMenu.getPreferredSize();
                }
                catch (Exception e) {
                    System.err.println("getPreferredSize() exception!");
                    e.printStackTrace();
                }
            }
        });
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Test finished");
    }
}

crashes systematically with:

Showing popup
Popup pref size is : java.awt.Dimension[width=102,height=21]
Text area pref size is : java.awt.Dimension[width=100,height=16]
Min size is : java.awt.Dimension[width=102,height=21]
Text area min size is : java.awt.Dimension[width=100,height=16]
getPreferredSize() exception!
java.lang.NullPointerException
    at javax.swing.BoxLayout.checkRequests(BoxLayout.java:466)
    at javax.swing.BoxLayout.preferredLayoutSize(BoxLayout.java:281)
    at javax.swing.plaf.basic.DefaultMenuLayout.preferredLayoutSize(DefaultMenuLayout.java:43)
    at java.awt.Container.preferredSize(Container.java:1597)
    at java.awt.Container.getPreferredSize(Container.java:1582)
    at javax.swing.JComponent.getPreferredSize(JComponent.java:1636)
    at TestPopup$1.run(TestPopup.java:26)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:646)
    at java.awt.EventQueue.access$000(EventQueue.java:84)
    at java.awt.EventQueue$1.run(EventQueue.java:607)
    at java.awt.EventQueue$1.run(EventQueue.java:605)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:616)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Test finished

It is difficult to see where this misuses the swing API, since it should run entirely in the EDT, and the preferred size should be set prior to the crash, in fact there doesn't seem to be a point in the above stack where the min/preferred size is set to null. Is it possible that another thread is being invoked here, or something is missing from the layout? The only bug report with this trace is marked fixed in my Java environment.

1
  • 1
    In the BoxLayout source code, checkRequests() iterates through all of a component's child components checking all of their size requirements. Perhaps your code is doing this when JPopUp menu objecdt has been created but before all of its child components have been created and rendered. Just a SWAG. Nov 22, 2013 at 0:01

2 Answers 2

3
  1. This condition occurs because the show method adjusts the width of the JTextArea subcomponent (in this example because the component has setLineWrap(true), show() changes the width width from 0 to 100) of a JPopupMenu. In this state the first call to getPreferredSize() on the JTextArea will update the size of its parent component, to cause the crash this first call must occur inside the JPopupMenu getPreferredSize() method.
  2. This crash can only occur when the JPopupMenu uses a DefaultMenuLayout, which is the L&F default here. This layout invalidates the JPopupMenu and then calls checkRequests(), other Layouts for the JPopupMenu do not.
  3. checkRequests() assumes the invariant that any visible children of an invalidated popup will also be invalid and requests their preferred size. However in this case the JTextArea subcomponent is valid, and so when its getPreferredSize() is called it and the condition in 1. occurs it invalidates itself and its parent component, setting the xChildren variable of the JPopupMenu to null and causing the crash.

From this, the possible solutions would seem to be:

  1. Changing the type of Layout for the JPopupMenu as explained in trashgod's answer.
  2. Making the children of the JPopupMenu visible prior to calling getPreferredSize().
  3. Ensuring the layout is updated before 3., either by calling getPreferredSize() on the subcomponent prior to any calls to getPreferredSize() on the JPopupMenu, by hiding and showing the component, or by explicitly setting the size.
  4. Invalidating the JTextArea which ensures that even when the layout changes inside a getPreferredSize() the parent component will not be updated.
1
  • 1
    Or you can queue the .getPreferredSize() call on the EDT which will delay it enough to prevent the error. Nov 22, 2013 at 18:00
2

This appears to be a feature of the DefaultMenuLayout, seen here. It extends BoxLayout and expects a non-null target Container, to which BasicPopupMenuUI defers. One alternative is to use a different layout, for example:

basicPopupMenu.setLayout(new GridLayout());
2
  • This got an upvote because it points to the root cause of the problem, but I can't accept it as the answer because as you say there are alternatives to changing the layout which is not an option in a lot of cases. Nov 22, 2013 at 5:10
  • heavyweight contianers must be pack(), maybe to change default LayoutManager
    – mKorbel
    Nov 22, 2013 at 7:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.