title: Null Pointer with JButton Array
body: <p>So far I have created a frame at adds my ControlPanel class, the class takes an int, and that int is used to fill an array with JButton objects which are the added to a Panel and displayed. </p>
  
  <p>However I am getting a Null Pointer error when I try to add JButtons to the array. I'm not sure why (since I thought null pointers were from when you try to reference something that isn't in the spot in the array?)</p>
  
  <p>Any help on this would be great appreciated. </p>
  
  <p>Error Message:</p>
  
  <pre><code>Exception in thread "main" java.lang.NullPointerException
      at elevator.ControlPanel.&lt;init&gt;(ControlPanel.java:22)
      at elevator.Elevator_Simulator.main(Elevator_Simulator.java:27)
  </code></pre>
  
  <p>Main Class</p>
  
  <pre><code>import javax.swing.*;
  import java.awt.*;
  import java.awt.event.*;
  import javax.swing.event.*;
  
  
  public class Elevator_Simulator extends JFrame {
  
  
  
  
      public static void main(String[] args) {
  
  
          JFrame frame = new JFrame ("Elevator Simulation");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(new ControlPanel(8));
          frame.pack();
          frame.setVisible(true);
  
  
      }
  
  }
  </code></pre>
  
  <p>Control Panel Class:</p>
  
  <pre><code>import java.awt.Color;
  import java.awt.event.*;
  import javax.swing.*;
  
  public class ControlPanel extends JPanel implements ActionListener {
  
      public JButton[] floorButton; // an array of floor buttons.
      public int[] floorIn;          // state of button. 0 == not click &gt;= 1 means clicked (how many times).
  
      public ControlPanel(int x) {
  
          JPanel floors = new JPanel();
  
          for (int i = 0; i &lt; x; i++) { // interates through user input and creates that many JButtons. 
              floorButton[i].add(new JButton("F" + Integer.toString(i))); // adds a button to button array. Sets text to F(floorNo).
              floors.add(floorButton[i]); // adds buttons to the floor panel.
              floorButton[i].addActionListener(this); // adds action listener to the buttons.
          }
      }
  
      public void actionPerformed(ActionEvent e) {
          for (int i = 0; i &lt; floorButton.length; i++) {
              if (e.getSource() == floorButton[i]) { // checks which button the event occured on.
  
                  floorButton[i].setBackground(Color.red); // sets button background to red.
                  floorButton[i].setText(floorButton[i].getText() + "(1)"); // marks button as clicked.
  
              }
          }
      }
  
  }
  </code></pre>