title: Java Swing open new content in same window
body: <p>I'm a student javadevelopment in Brussels. 
  The focus of the course lies on backend and webapp's, now we're currently covering spring(core, enterprise and mvc, we going to skip security and messaging). </p>
  
  <p>I would like to be able to create graphic stand alone app's to, so I started a side project in swing, for a non-profit historical organisation I want to wright a app that connect's with a local database and let's them manage their members and the libary. Secondary there's going to be a webapp that lets de websitevisitors search the libary for availability and reserve the items.   </p>
  
  <p>I started with the adminapp (to manage members and the libary).
  It starts with a simple userloginform a textfield user, a other password and a loginbutton. On click it's stores the username and password and should open a other JPanel in the same frame, that's bassicly how the user is going to navigate through the app. 
  When I click the loginbutton a new empty frame opens (what it shouldn't), when I maximize my new frame and minimalize it again, my old frame is showing what it's supposed to. 
  I know the way I call my second Jpanel is not the most elegant way, but I had no pre knolledge of swing starting this, I just started experimenting and puzzled bits and piece together on the internet.</p>
  
  <p>Anyway, this is the nullpointer exception I get:</p>
  
  <pre><code>Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
  at  com.intellij.uiDesigner.core.GridLayoutManager.addLayoutComponent
  (GridLayoutManager.java:134)
  at java.awt.Container.addImpl(Container.java:1127)
  at java.awt.Container.add(Container.java:417)
  at be.kokw.views.MainApp$1.actionPerformed(MainApp.java:48)
  at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
  at javax.swing.AbstractButton$Handler.actionPerformed
  (AbstractButton.java:2348)
  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:6533)
  at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
  at java.awt.Component.processEvent(Component.java:6298)
  at java.awt.Container.processEvent(Container.java:2236)
  at java.awt.Component.dispatchEventImpl(Component.java:4889)
  at java.awt.Container.dispatchEventImpl(Container.java:2294)
  at java.awt.Component.dispatchEvent(Component.java:4711)
  at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
  at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
  at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
  at java.awt.Container.dispatchEventImpl(Container.java:2280)
  at java.awt.Window.dispatchEventImpl(Window.java:2746)
  at java.awt.Component.dispatchEvent(Component.java:4711)
  at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
  at java.awt.EventQueue.access$500(EventQueue.java:97)
  at java.awt.EventQueue$3.run(EventQueue.java:709)
  at java.awt.EventQueue$3.run(EventQueue.java:703)
  at java.security.AccessController.doPrivileged(Native Method)
  at java.security.ProtectionDomain$JavaSecurityAccessImpl
  .doIntersectionPrivilege(ProtectionDomain.java:80)
  at java.security.ProtectionDomain$JavaSecurityAccessImpl
  .doIntersectionPrivilege(ProtectionDomain.java:90)
  at java.awt.EventQueue$4.run(EventQueue.java:731)
  at java.awt.EventQueue$4.run(EventQueue.java:729)
  at java.security.AccessController.doPrivileged(Native Method)
  at java.security.ProtectionDomain$JavaSecurityAccessImpl.
  doIntersectionPrivilege(ProtectionDomain.java:80)
  at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
  at java.awt.EventDispatchThread.pumpOneEventForFilters
  (EventDispatchThread.java:201)
  at java.awt.EventDispatchThread.pumpEventsForFilter
  (EventDispatchThread.java:116)
  at java.awt.EventDispatchThread.pumpEventsForHierarchy
  (EventDispatchThread.java:105)
  at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
  at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
  at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
  </code></pre>
  
  <p>My MainClass:</p>
  
  <pre><code>public class MainApp{
  private JButton login;
  private JPanel panel1;
  
  private JLabel labelForUser;
  private JTextField userField;
  private JLabel labelForPassword;
  private JTextField passwordField;
  private String user;
  private String password;
  static JFrame frame = new JFrame("KokwApp");
  
  public String getUser() {
      return user;
  }
  
  public String getPassword() {
      return password;
  }
  
  
  public MainApp() {
  
      login.addActionListener(new ActionListener() {
          /**
           * Invoked when an action occurs.
           *
           * @param e
           */
          @Override
          public void actionPerformed(ActionEvent e) {
              user = userField.getText();
              password = passwordField.getText();
              DbSelection db = new DbSelection();
              frame.getContentPane().removeAll();
              frame.getContentPane().add(db.getPanel1());
              frame.repaint();
  
  
  
          }
      });
  }
  
  public static void main(String[] args) {
      frame.setContentPane(new MainApp().panel1);
      frame.pack();
      frame.setVisible(true);
  }
  }
  </code></pre>
  
  <p>I asked my teacher today...
  His answer: I don't like to use swing. Read up on javafx... Lol</p>
  
  <p>So any tips on what's wrong would be welcome.
  Just to be exact I use intelij.
  Finally, sorry for my crappy gramar, but as I said in the beginning, I'm Belgian</p>