I'm getting a Null Pointer Exception and can't figure out why. Here's the error:

at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at TravelExpenses.buildPanel(TravelExpenses.java:44)
at TravelExpenses.<init>(TravelExpenses.java:25)
at TravelExpenses.main(TravelExpenses.java:105)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)

It seems like the issue is here:
    for(int i = 0; i < numElements; i++){
      panelArray[i] = generatePanel(panelLabelArray[i]);
But I don't see any other way to use a function to create these sub-panels.

import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.text.DecimalFormat;

public class TravelExpenses extends JFrame{
  private JPanel mainPanel, subPanel, southPanel, panel, 
    dayPanel, airfarePanel, rentalCarPanel; // Panels to hold components
  private JLabel dayLabel, airfareLabel, rentalCarLabel, privateMilesLabel, 
    parkingLabel, taxiLabel, registrationLabel, lodgingLabel; // Messages for user
  private JTextField dayTextField, airfareTextField, rentalCarTextField, privateMilesTextField, 
    parkingTextField, taxiTextField, registrationTextField, lodgingTextField; // User Input
  private JButton calcButton, restButton; // Reference for the Calculate Button
  private int days;
  private double airfare, rentalCar, privateMiles, parking, taxi, registration, lodging;
  private final double MEALS = 37.00;
  private final double PARKING_FEES = 10.00;
  private final double TAXI_COSTS = 20.00;
  private final double LODGING_COSTS = 95.00;
  private final double PRIVATE_MILES_COST = 0.27;
  private final int WINDOW_WIDTH = 400;
  private final int WINDOW_HEIGHT = 200;
  public TravelExpenses(){
    super("Travel Expense Calculator"); // Call JFrame Constructor
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Set the 'X' button to close and terminate the program
    buildPanel(); // Call the buildPanel Method to put all the pieces together
    add(mainPanel); // Add the Panel constructed from buildPanel to the JFrame.
    setSize(WINDOW_WIDTH, WINDOW_HEIGHT); // Set Window Size
    setVisible(true); // Make the JFrame Visible
    setLocationRelativeTo(null); // Center window in middle of screen
  private void buildPanel(){
    //Set-Up Input Fields via Arrays
    JPanel[] panelArray = {dayPanel, airfarePanel, rentalCarPanel};
    JTextField[] panelTextFieldArray = {dayTextField, airfareTextField, rentalCarTextField};
    String[] panelLabelArray = {"Length of Trip (in Days):", "Airfare (if any):", "Rental Car Fee (if any):"};
    //Calculate Length of Array
    int numElements = panelArray.length;
    //Loop through arrays and generate each panel
    for(int i = 0; i < numElements; i++){
      panelArray[i] = generatePanel(panelLabelArray[i]);
    //Add Each input panel to the main sub-panel 
    subPanel = new JPanel();
    subPanel.setLayout(new BoxLayout(subPanel, BoxLayout.Y_AXIS));
    for(int j = 0; j < numElements; j++){
    //Calculare Button
    calcButton = new JButton("Calculate");
    calcButton.setSize(80, 200);
    // Create a new Listener to detect the click of the Calculate button
    calcButton.addActionListener(new CalcButtonListener());
    //Create Calculate Button Panel
    southPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 15));
    //Initiate a new instance of a JPanel and add all of the elements to it
    mainPanel = new JPanel();
    mainPanel.setLayout(new BorderLayout());
    mainPanel.add(subPanel, BorderLayout.NORTH);
    mainPanel.add(southPanel, BorderLayout.SOUTH);
  private JPanel generatePanel(String labelText){
    JLabel label = new JLabel(labelText);
    panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
    panel.add(Box.createRigidArea(new Dimension(5,0)));
    return panel;
  private class CalcButtonListener implements ActionListener{
    public void actionPerformed(ActionEvent e){
      //Get the User submitted data
      String input = dayTextField.getText();
      //Calculate the Temperature in Celsius and round to the hundreths place
      double c = calcTemp(input);
      DecimalFormat df = new DecimalFormat("#.##");      
      c = Double.valueOf(df.format(c));
      // Show Final Message to User after Calculation and Button Click
      JOptionPane.showMessageDialog(null, input + " Fahrenheit is " + c + " Degrees Celsius");
    // Function to Convert temperature
    private double calcTemp(String temp){
      return ((Double.parseDouble(temp) - 32)*5/9);
  public static void main(String args[]){
    new TravelExpenses();