title: NullPointerException while adding an object to an array
body: <p>Alright so I'm having a problem with some code that is generating a null pointer exception, meaning that my code is attempting to use a null value where it should be using an object (Straight out of the API). What I'm doing is I have a class called Hotel that has a constructor which assigns a string value to a variable and creates an array of type Room, an object. I know this should be straight forward yet for the life of me I cannot figure this out.</p>
  
  <p>I'll try and keep the code as minimalist as possible:</p>
  
  <p>Constructor for an object, Hotel, that creates a new array of size NUM_ROOMS</p>
  
  <p><strong>Here are the included updates, I still am stuck with the Null pointer problem unfortunately</strong></p>
  
  <pre><code>public Hotel(String hotelName)
  {
      name = hotelName;
      theRoom = new Room[NUM_ROOMS];
  }
  </code></pre>
  
  <p>This constructor creates an array of type Room, whose parameters are assigned to the variables</p>
  
  <pre><code>public Room(String guestName, int partySize, int bedCountRequired,
          double ratePerDay, int daysStaying)
  {
      guest = guestName;
      number = partySize;
      numBeds = bedCountRequired;
      rate = ratePerDay;
      numOfDays = daysStaying;
  }
  </code></pre>
  
  <p>This following method is supposed to:</p>
  
  <p>1) create a new instance of the object Room using the supplied parameters</p>
  
  <p>2) assign an array reference to that instance at the index roomsRented (defined as zero when the code is first initialized then increases by one after)</p>
  
  <pre><code>public void addReservation(String guestName, int partySize,
          int bedCountRequired, double ratePerDay, int daysStaying)
  {
      Room roomHolder = new Room(guestName, partySize, bedCountRequired,
              ratePerDay, daysStaying);
  
      theRoom[roomsRented] = roomHolder;
  
      roomsRented++;
  }
  </code></pre>
  
  <p>The problem that I am encountering is that </p>
  
  <pre><code>theRoom[roomsRented] = roomHolder;
  </code></pre>
  
  <p>is appearing to cause a null pointer exception. I am also getting an error from Eclipse that  the array variable theRoom is unused despite that I use it in the aforementioned method. This leads me to believe that I am not referencing that object correctly. However, I don't know why that reference is incorrect, even if I substitute in theRoom[0], the compiler still gives me a null pointer. I don't see any problems with my array construction which to me means that the only problem that could cause a null pointer exception is if I don't reference any values in my array.</p>
  
  <p>Any thoughts?</p>
  
  <p>Thanks in advance for any contributions and apologies if the answer is painfully simple.</p>
  
  <p><strong>EDIT***</strong>
  Thank you for your help, the problem with the variable not being found has been resolved but everytime I try and reference the object in the array I still get a nullpointer exception. Here is an example of how I'm trying to reference the array:</p>
  
  <pre><code>public double getTotalRentalSales()
  {
      // Iterate through the array
      for (int i = 0; i &lt; theRoom.length; i++)
      {
          // sum the amount money received per rental
          totalRate = totalRate + theRoom[i].getRate();
      }
      // Return total
      return totalRate;
  }
  </code></pre>
  
  <p>where getRate() is a method that returns the variable rate from the object Room</p>
  
  <pre><code>totalRate = totalRate + theRoom[i].getRate();
  </code></pre>
  
  <p>is causing the null pointer in this situation. Is there a problem with my object reference?</p>