title: "*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'"
body: <p>I know this is a common question on Stack Overflow, but unfortunately none have been able to lead me to my solution.</p>
  
  <p>I'm trying to list the files in my documents directory in a <code>UITableView</code>. (I also have another tableview displaying devices I'm connected to)</p>
  
  <p>I know I'm getting this error because its saying that my array has 0 objects, and I'm trying to access the object at index 1.</p>
  
  <p>But I have a file in my documents directory.
  If I have 2 files, when I delete 1, I get this error. When the app is relaunched, it shows the <code>UITableView</code> correctly with the file deleted.</p>
  
  <p>I got my code from here:
  <strong><em><a href="https://stackoverflow.com/q/17392769/3544744">Objective-C: How to list files from documents directory into a UITableView?</a></em></strong></p>
  
  <p>this is my content:</p>
  
  <pre><code>-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
      if (tableView == _tblConnectedDevices){
      return [_arrConnectedDevices count];
      }
      if ([filePathsArray count] &gt; 0){
          return [filePathsArray count];
      }
      else 
      return 1;
  }
  
  
  -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
      if (tableView == _tblConnectedDevices){
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
  
      if (cell == nil) {
          cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellIdentifier"];
      }
  
      cell.textLabel.text = [_arrConnectedDevices objectAtIndex:indexPath.row];
  
      return cell;
      } else {
          UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
          NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
          NSString *documentsDirectory = [paths objectAtIndex:0];
          filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory  error:nil];
          NSString *last = [documentsDirectory stringByAppendingPathComponent:[filePathsArray objectAtIndex:indexPath.row]];
          NSString *last2 = [[last lastPathComponent] stringByDeletingPathExtension];
          cell.textLabel.text = last2;
          return cell;
      }
  }
  </code></pre>
  
  <p>My delete void:</p>
  
  <pre><code>-(IBAction)deleteFile:(id)sender{
      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                           NSUserDomainMask,
                                                           YES);
      NSString *fullPath = [[paths lastObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.txt", fileName]];
      NSError *error;
          [[NSFileManager defaultManager] removeItemAtPath:fullPath error:&amp;error];
      [_cellView reloadData];
  }
  </code></pre>