title: Adding JTextField to JPanel on button click (IntelliJ GUI Designer)
body: <p>I realize this question has been asked before but none of the solutions I tried worked. I am trying to swap content in a JPanel based on user input. 
  For example, I would like to remove two buttons and add several JTextField. </p>
  
  <p>No matter what I do, I get a null pointer exception. I tried creating the element on the form and simply hiding it (setVisible(false)) and setting it to visible on the button click rather than creating it. That returned a nullptr as well. </p>
  
  <p>Here is the action event listener for the button:</p>
  
  <pre><code> importOnlineButton.addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent event) {
              mainContentPanel.remove(importOfflineButton);
              mainContentPanel.remove(importOnlineButton);
  
              JTextField test = new JTextField(1);
              mainContentPanel.add(test);
              //pack();
              revalidate();
              repaint();
           }
        });
  </code></pre>
  
  <p>Here is my full code:</p>
  
  <pre><code>package com.arcologydesigns.Views;
  
  import javax.swing.*;
  import java.awt.*;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.awt.event.ItemEvent;
  import java.awt.event.ItemListener;
  import java.awt.event.KeyEvent;
  import java.awt.event.MouseAdapter;
  import java.awt.event.MouseEvent;
  import java.net.URL;
  
  import static java.awt.event.ActionEvent.*;
  
  /**
   * EducatorMainForm created by Borislav S. on 7/11/2015 @ 9:57 PM.
   */
  public class EducatorMainForm extends JFrame {
  
     private ImageIcon img;
     private JPanel educatorMainPanel;
     private JLabel userIdLabel;
     private JButton importOnlineButton;
     private JButton importOfflineButton;
     private JPanel mainContentPanel;
     private JPanel userIdPanel;
     private JLabel helpText1;
     private JLabel helpText2;
     private String imgUrl;
     private JPopupMenu pmenu;
  
     private class MenuItemAction extends AbstractAction {
  
        public MenuItemAction(String text, Icon icon,
                              Integer mnemonic) {
           super(text);
  
           putValue(SMALL_ICON, icon);
           putValue(MNEMONIC_KEY, mnemonic);
        }
  
        @Override
        public void actionPerformed(ActionEvent e) {
  
           System.out.println(e.getActionCommand());
        }
     }
  
     public EducatorMainForm(char userType) {
        educatorMainPanel.setSize(300, 300);
        //setLocationRelativeTo(null);
  
        switch (userType) {
           case 'S':
              imgUrl = "images/educator_logo_student.jpg";
              break;
           case 'I':
              imgUrl = "images/educator_logo_instructor.jpg";
              break;
           case 'G':
              imgUrl = "images/educator_logo_guest.jpg";
              break;
           default:
              imgUrl = "images/educator_logo_guest.jpg";
              System.out.print("Invalid user type.");
              break;
        }
  
        createAndShowGUI();
        createPopupMenu();
        createMenuBar();
  
  
        importOnlineButton.addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent event) {
              mainContentPanel.remove(importOfflineButton);
              mainContentPanel.remove(importOnlineButton);
  
              JTextField test = new JTextField(1);
              mainContentPanel.add(test);
              pack();
  
              revalidate();
              repaint();
  
           }
        });
     }
  
     private void createAndShowGUI() {
  
        // Set title and default close operation
        setTitle("Educator Productivity Toolkit - EPT v0.1");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
  
        // Set a background for JFrame
        ClassLoader cldr = this.getClass().getClassLoader();
  
        URL imageURL = cldr.getResource(imgUrl);
        if (imageURL != null) {
           img = new ImageIcon(imageURL);
        }
  
        setContentPane(new JLabel(img));
  
        // Set some layout, say FlowLayout
        setLayout(new FlowLayout());
  
        // Set the background, black with 125 as alpha value. This is less transparent
        if(educatorMainPanel != null) {
           educatorMainPanel.setBackground(new Color(33, 71, 116, 165));
  
           // Set some size to the panels
           educatorMainPanel.setPreferredSize(new Dimension(1920, 1080));
           //p2.setPreferredSize(new Dimension(250,150));
  
           // Add the panels to the JFrame
           add(educatorMainPanel);
        }
  
        // Set the size of the JFrame and make it visible
        setExtendedState(JFrame.MAXIMIZED_BOTH);  // set to full screen
        setVisible(true);
     } // end createAndShowGUI()
  
     private void createPopupMenu() {
  
        pmenu = new JPopupMenu();
  
        JMenuItem maxMi = new JMenuItem("Maximize");
        maxMi.addActionListener(new ActionListener() {
  
           @Override
           public void actionPerformed(ActionEvent e) {
  
              if (getExtendedState() != JFrame.MAXIMIZED_BOTH) {
                 setExtendedState(JFrame.MAXIMIZED_BOTH);
              }
  
           }
        });
  
        pmenu.add(maxMi);
  
        JMenuItem quitMi = new JMenuItem("Quit");
        quitMi.addActionListener(new ActionListener() {
  
           @Override
           public void actionPerformed(ActionEvent e) {
              System.exit(0);
           }
        });
  
        /**
         * Lambda expression alternative to above function:
         * quitMi.addActionListener((e) -&gt; { System.exit(0); });
         * Only available in Java 8
         * */
  
        pmenu.add(quitMi);
  
        addMouseListener(new MouseAdapter() {
  
           @Override
           public void mouseReleased(MouseEvent e) {
  
              if (e.getButton() == MouseEvent.BUTTON3) {
                 pmenu.show(e.getComponent(), e.getX(), e.getY());
              }
           }
        });
     }
  
     private void createMenuBar() {
        Icon iconNew =  UIManager.getIcon("FileChooser.newFolderIcon");
        Icon iconSave =  UIManager.getIcon("FileView.floppyDriveIcon");
        Icon iconOpen =  UIManager.getIcon("Tree.timeFactor");
        Icon iconExit = UIManager.getIcon("InternalFrame.closeIcon");
  
        Icon iconOptions = UIManager.getIcon("ToolBar.handleIcon");
  
  
        JMenuBar menubar = new JMenuBar();
  
        JMenu fileMenu = new JMenu("File");
        fileMenu.setMnemonic(KeyEvent.VK_F);
  
        JMenuItem newMi = new JMenuItem(new MenuItemAction("New", iconNew, KeyEvent.VK_N));
        JMenuItem openMi = new JMenuItem(new MenuItemAction("Open", iconOpen, KeyEvent.VK_O));
        JMenuItem saveMi = new JMenuItem(new MenuItemAction("Save", iconSave, KeyEvent.VK_S));
        JMenuItem optionsMi = new JMenuItem(new MenuItemAction("Options", iconOptions, KeyEvent.VK_O));
  
        JMenuItem exitMi = new JMenuItem("Exit", iconExit);
        exitMi.setMnemonic(KeyEvent.VK_E);
        exitMi.setToolTipText("Exit application");
        exitMi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, CTRL_MASK));
  
        exitMi.addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent event) {
              System.exit(0);
           }
        });
  
        fileMenu.add(newMi);
        fileMenu.add(openMi);
        fileMenu.add(saveMi);
        fileMenu.addSeparator();
        fileMenu.add(exitMi);
  
        JMenu viewMenu = new JMenu("View");
        viewMenu.setMnemonic(KeyEvent.VK_V);
  
        JMenu toolsMenu = new JMenu("Tools");
        toolsMenu.add(optionsMi);
        optionsMi.addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent event) {
              OptionsForm o = new OptionsForm();
           }
        });
  
        JMenu helpMenu = new JMenu("Help");
  
        JCheckBoxMenuItem sbarMi = new JCheckBoxMenuItem("Show statubar");
        sbarMi.setMnemonic(KeyEvent.VK_S);
        sbarMi.setDisplayedMnemonicIndex(5);
        sbarMi.setSelected(true);
  
        sbarMi.addItemListener(new ItemListener() {
  
           @Override
           public void itemStateChanged(ItemEvent e) {
  
              if (e.getStateChange() == ItemEvent.DESELECTED) {
                 helpText1.setVisible(false);
                 helpText2.setVisible(false);
              } else {
                 helpText1.setVisible(true);
                 helpText2.setVisible(true);
              }
  
           }
  
        });
  
        viewMenu.add(sbarMi);
        menubar.add(fileMenu);
        menubar.add(viewMenu);
        menubar.add(toolsMenu);
        menubar.add(Box.createHorizontalGlue());
        menubar.add(helpMenu);
  
        setJMenuBar(menubar);
     }
  
     public void setUserIdLabel(String _userIdLabel) {
        this.userIdLabel.setText(_userIdLabel);
     }
  
     public void setImageURL(String _imageURL) {
        imgUrl = _imageURL;
     }
  
  } // end class EducatorMainForm
  </code></pre>
  
  <p>Here is the exception (Null Pointer):</p>
  
  <pre><code>Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to com.intellij.uiDesigner.core.GridConstraints
      at com.intellij.uiDesigner.core.GridLayoutManager.addLayoutComponent(GridLayoutManager.java:133)
      at java.awt.Container.addImpl(Container.java:1120)
      at java.awt.Container.add(Container.java:966)
      at com.arcologydesigns.Views.EducatorMainForm$1.actionPerformed(EducatorMainForm.java:82)
      at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
      at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
      at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
      at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
      at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
      at java.awt.Component.processMouseEvent(Component.java:6505)
      at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
      at java.awt.Component.processEvent(Component.java:6270)
      at java.awt.Container.processEvent(Container.java:2229)
      at java.awt.Component.dispatchEventImpl(Component.java:4861)
      at java.awt.Container.dispatchEventImpl(Container.java:2287)
      at java.awt.Component.dispatchEvent(Component.java:4687)
      at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
      at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
      at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
      at java.awt.Container.dispatchEventImpl(Container.java:2273)
      at java.awt.Window.dispatchEventImpl(Window.java:2719)
      at java.awt.Component.dispatchEvent(Component.java:4687)
      at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
      at java.awt.EventQueue.access$200(EventQueue.java:103)
      at java.awt.EventQueue$3.run(EventQueue.java:694)
      at java.awt.EventQueue$3.run(EventQueue.java:692)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
      at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
      at java.awt.EventQueue$4.run(EventQueue.java:708)
      at java.awt.EventQueue$4.run(EventQueue.java:706)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
      at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
      at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
      at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
      at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
      at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
  </code></pre>