javaeclipseswingmiglayoutwindowbuilder

JTextArea in MigLayout causes NullPointerException


I'm not a pro with swing layout managers and this behavior is completely baffling me. Essentially I can't add a JTextArea or JTextPane to my MigLayout without getting a NullPointerException.

I created a class that extends JPanel:

public ComponentPanel(TestComponent testComponent) {
    setLayout(new MigLayout("", "[]", "[][][][][][]"));

    JLabel lblNewLabel = new JLabel("New label");
    add(lblNewLabel, "cell 0 0");

    JButton button = new JButton("New button");
    add(button, "cell 0 3");

    JButton btnNewButton = new JButton("New button");
    add(btnNewButton, "cell 0 5");
}

This works fine. However, if I try adding a textArea, it will throw an exception when I try to build:

public ComponentPanel(TestComponent testComponent) {
    setLayout(new MigLayout("", "[grow]", "[][grow][][][][]"));

    JLabel lblNewLabel = new JLabel("New label");
    add(lblNewLabel, "cell 0 0");

    JTextArea textArea = new JTextArea();
    add(textArea, "cell 0 1,grow");

    JButton button = new JButton("New button");
    add(button, "cell 0 3");

    JButton btnNewButton = new JButton("New button");
    add(btnNewButton, "cell 0 5");
}

I'm using the Window Designer in eclipse, so I'm assuming something is horking up the MigLayout constructor? I've been beating my head on this for awhile and all I've managed to figure out is that adding textArea or textPanes causes the following exception:

java.lang.NullPointerException at javax.swing.BoxLayout.preferredLayoutSize(Unknown Source) at java.awt.Container.preferredSize(Unknown Source) at java.awt.Container.getPreferredSize(Unknown Source) at javax.swing.JComponent.getPreferredSize(Unknown Source) at javax.swing.JRootPane$RootLayout.preferredLayoutSize(Unknown Source) at java.awt.Container.preferredSize(Unknown Source) at java.awt.Container.getPreferredSize(Unknown Source) at javax.swing.JComponent.getPreferredSize(Unknown Source) at java.awt.BorderLayout.preferredLayoutSize(Unknown Source) at java.awt.Container.preferredSize(Unknown Source) at java.awt.Container.getPreferredSize(Unknown Source) at net.miginfocom.swing.MigLayout.adjustWindowSize(Unknown Source) at net.miginfocom.swing.MigLayout.layoutContainer(Unknown Source) at net.miginfocom.swing.MigLayout.preferredLayoutSize(Unknown Source) at java.awt.Container.preferredSize(Unknown Source) at java.awt.Container.getPreferredSize(Unknown Source) at javax.swing.JComponent.getPreferredSize(Unknown Source) at javax.swing.BoxLayout.checkRequests(Unknown Source) at javax.swing.BoxLayout.layoutContainer(Unknown Source) at java.awt.Container.layout(Unknown Source) at java.awt.Container.doLayout(Unknown Source) at java.awt.Container.validateTree(Unknown Source) at java.awt.Container.validateTree(Unknown Source) at java.awt.Container.validateTree(Unknown Source) at java.awt.Container.validateTree(Unknown Source) at java.awt.Container.validate(Unknown Source) at java.awt.Window.show(Unknown Source) at java.awt.Component.show(Unknown Source) at java.awt.Component.setVisible(Unknown Source) at java.awt.Window.setVisible(Unknown Source) at com.soartech.ssim.testbed.gui.TestbedApplication$1.run(TestbedApplication.java:25) at java.awt.event.InvocationEvent.dispatch(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$000(Unknown Source) at java.awt.EventQueue$1.run(Unknown Source) at java.awt.EventQueue$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source) Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at javax.swing.SizeRequirements.calculateTiledPositions(Unknown Source) at javax.swing.BoxLayout.layoutContainer(Unknown Source) at java.awt.Container.layout(Unknown Source) at java.awt.Container.doLayout(Unknown Source) at java.awt.Container.validateTree(Unknown Source) at java.awt.Container.validateTree(Unknown Source) at java.awt.Container.validateTree(Unknown Source) at java.awt.Container.validateTree(Unknown Source) at java.awt.Container.validate(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$000(Unknown Source) at java.awt.EventQueue$1.run(Unknown Source) at java.awt.EventQueue$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source) at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$2.run(Unknown Source) at java.awt.EventQueue$2.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)


Solution

  • The problem ended up being that I needed to let SwingUtilities create my panels for me, because they were being programmatically generated.

        Runnable doWorkRunnable = new Runnable() {
            public void run() {
                for(TestComponent c : Config.getComponents()){
                    frame.getContentPane().add(new ComponentPanel(c));
                    frame.getContentPane().add(Box.createRigidArea(new Dimension(5,0)));
                }
            }
        };
    SwingUtilities.invokeLater(doWorkRunnable);
    

    Now that this is in place, I can include text areas in my MigLayout without any problems.