title: UITableView insertRowsAtIndexPaths throwing __NSArrayM insertObject:atIndex:'object cannot be nil' error
body: <p>I am trying to insert an item to my table view dynamically. My app has a chat section, where it displays the old (loaded from server before initializing the view controller) messages in section 0, and displays just sent/received messages (where it is initially zero) in section 1. When the view is loaded, all the "old" messages are loaded and displayed, no problem is there. The problem starts when I try to insert rows. Here is what I am doing:</p>
  
  <ul>
  <li>I am first updating my table view's data source by adding an extra item: <code>[newMessages addObject:newMessage];</code> (newMessage is an instance of my custom message object, and newMessages is my data source). I verify that my data source now has 1 item (which was 0 before adding).</li>
  <li><p>I then call the following code:</p>
  
  <pre><code>[self.chatTableView beginUpdates];
  [self.chatTableView insertRowsAtIndexPaths:@[[NSIndexPath 
      indexPathForRow:newMessages.count - 1 inSection:1]] 
      withRowAnimation:UITableViewRowAnimationBottom];
  [self.chatTableView endUpdates];
  </code></pre></li>
  </ul>
  
  <p>My app crashes at <code>endUpdates</code> method, giving me this error: <code>*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'</code></p>
  
  <p>I've immediately checked if <code>newMessage</code> is <code>nil</code> or not, it's <strong>not</strong> <code>nil</code> (and re-checked my data source) so data source is not the problem. I've thought that <code>indexPathForRow:newMessages.count - 1</code> could be the problem and tried different values (count - 2, count, count + 1) just in case I was missing something. In those cases, I'm getting another error: <code>'NSInternalInconsistencyException', reason: 'attempt to insert row 1 into section 1, but there are only 1 rows in section 1 after the update'</code>. The error says it all, so the problem is not something with <code>indexPathForRow:newMessages.count - 1</code> either.</p>
  
  <p>I've added breakpoints into the <code>-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath</code> method to see when it's exactly called and what it returns. It seems that the method is not called at all, the breakpoint is not hitting (it DOES hit when view is first loaded and loads the initial data correctly, and I'm not setting data source anywhere, so delegates/data sources are also connected correctly). Immediately I've checked other methods:</p>
  
  <pre><code>-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
      return 2;
  }
  
  -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
      if(section == 0){
          return @"Eski mesajlar";
      }else{
          return @"Yeni mesajlar";
      }
  }
  </code></pre>
  
  <p>Those methods return correct values. I've put a breakpoint in <code>-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView</code> to see when it's called. It apparently <em>is</em> called inside the <code>[self.chatTableView endUpdates];</code> method (as seen from the thread/queue's call stack), but then <code>[self.chatTableView endUpdates];</code> immediately throws the error without even entering <code>-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath</code> method. I've seen examples (and a few others) such as:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/6943470/how-to-properly-use-insertrowsatindexpaths">how to properly use insertRowsAtIndexPaths?</a></li>
  <li><a href="https://stackoverflow.com/questions/16848913/error-creating-row-in-tableview">Error creating row in tableview</a></li>
  <li><a href="https://stackoverflow.com/questions/9874917/add-cell-to-bottom-of-uitableview-in-ios">Add cell to bottom of UITableView in iOS</a></li>
  <li><a href="https://stackoverflow.com/questions/14035652/tableview-crashes-when-moving-more-than-half-the-cells-to-another-section">Tableview crashes when moving more than half the cells to another section</a> </li>
  </ul>
  
  <p>I've read the answers there, but none of them helped me. What am I doing wrong?</p>