title: NullPointerException in a Java Swing application
body: <p>I have a class that implements card layout:</p>
  
  <p><strong>CardWindow.java</strong></p>
  
  <pre><code>import java.awt.CardLayout;
  import java.awt.EventQueue;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  
  import javax.swing.JButton;
  import javax.swing.JFrame;
  import javax.swing.JPanel;
  
  import extraction.gui.Diagram;
  import extraction.main.Main;
  
  public class CardWindow extends JPanel{
  
  /**
   * 
   */
  private static final long serialVersionUID = 1L;
  private JFrame f;
  private JPanel panel_1, panel_2;
  private JButton btnNext, btnBack;
  
  /**
   * Launch the application.
   */
  public static void main(String[] args) {
      EventQueue.invokeLater(new Runnable() {
          public void run() {
              try {
                  CardWindow window = new CardWindow();
                  window.f.setVisible(true);
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
      });
  }
  
  /**
   * Create the application.
   */
  public CardWindow() {
      initialize();
  }
  
  /**
   * Initialize the contents of the frame.
   */
  private void initialize() {
      f = new JFrame();
      f.setBounds(100, 100, 1000, 480);
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      f.getContentPane().setLayout(new CardLayout(0, 0));
  
      panel_1 = new Main_1();
      panel_2 = new Snd();
      f.getContentPane().add(panel_1);
      f.getContentPane().add(panel_2);
      panel_1.setVisible(true);
      panel_2.setVisible(false);
  
      btnNext = new JButton("Next");
      btnNext.setBounds(829, 417, 161, 29);
      panel_1.add(btnNext);
      btnNext.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
              panel_1.setVisible(false);
              panel_2.setVisible(true);
          }
      });
  
      btnBack = new JButton("Back");
      btnBack.setBounds(919, 5, 75, 29);
      panel_2.add(btnBack);
      btnBack.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
              panel_2.setVisible(false);
              panel_1.setVisible(true);
          }
      });
  
  }
  }
  </code></pre>
  
  <p><strong>Main_1.java</strong></p>
  
  <pre><code>import javax.swing.JPanel;
  import javax.swing.JTextField;
  import javax.swing.JButton;
  import javax.swing.JLabel;
  
  
  public class Main_1 extends JPanel {
  private JTextField textField;
  
  /**
   * Create the panel.
   */
  public Main_1() {
      setLayout(null);
  
      textField = new JTextField();
      textField.setBounds(131, 69, 134, 28);
      add(textField);
      textField.setColumns(10);
  
      JButton btnCalculate = new JButton("Calculate");
      btnCalculate.setBounds(131, 122, 117, 29);
      add(btnCalculate);
  btnCalculate.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
              Calculate cal = new Calculate(textField.getText());
          }
      });
  
  
  }
  }
  </code></pre>
  
  <p><strong>Snd.java</strong></p>
  
  <pre><code>import javax.swing.JPanel;
  import javax.swing.JLabel;
  
  
  public class Snd extends JPanel {
  
  /**
   * Create the panel.
   */
  public Snd() {
      setLayout(null);
  
      Calculate _cal = new Calculate();
  
      JLabel label = new JLabel("New label");
      label.setBounds(180, 120, 61, 16);
      add(label);
      label.setText(_cal.getNum().get(0).toString());
  
  }
  
  }
  </code></pre>
  
  <p><strong>Calculate.java</strong></p>
  
  <pre><code>import java.util.ArrayList;
  import java.util.List;
  
  
  public class Calculate {
  
  private List&lt;Integer&gt; abc;
  
  public Calculate () {
  
  }
  
  public Calculate(String number) {
      abc = new ArrayList&lt;Integer&gt;();
      int num = Integer.valueOf(number);
      for (int i=0;i&lt;num;i++) {
          abc.add(i);
      }
  System.out.println(abc);
  }
  
  public List&lt;Integer&gt; getNum() {
      return abc;
  }
  }
  </code></pre>
  
  <p>I have two other <code>JPanel</code> classes: <code>Main_1.java</code>, <code>Snd.java</code></p>
  
  <p>Basically when I open the application, it will show <code>Main_1.java</code>. I enter some values and it run something in <code>Calculate.java</code>. Then when I click the next button <code>btnNext</code>, it's supposed to display the processed data onto the next card <code>Snd.java</code>. But I got <code>java.lang.NullPointerException</code> when trying to run the application.</p>
  
  <p>NPE at this line <code>label.setText(_cal.getNum().get(0).toString());</code></p>
  
  <p><strong>Error</strong></p>
  
  <pre><code>java.lang.NullPointerException
  at Snd.&lt;init&gt;(Snd.java:18)
  at CardWindow.initialize(CardWindow.java:59)
  at CardWindow.&lt;init&gt;(CardWindow.java:46)
  at CardWindow$1.run(CardWindow.java:33)
  at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
  at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
  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$1.doIntersectionPrivilege(ProtectionDomain.java:75)
  at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
  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><strong>I understand why it's giving NPE but I don't know how to solve it. When I run the program, I don't understand why it's trying to run <code>Snd.java</code> as well because I didn't click any button to go to the next card, which is <code>Snd.java</code>.</strong></p>
  
  <p>This is just a runnable example which is to show what I want to achieve. Basically I want to process something in <code>Main_1.java</code> and transfer the data to <code>Snd.java</code> where I will do another process there.</p>