title: App crashing: Index out of bound at random places
body: <p>My app is crashing at different places but the error is same as below only the index changes sometimes, any idea why is that happening I am attaching my screenshot as well:</p>
  
  <pre><code>*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds for empty array'
  *** First throw call stack:
  (0x20d4012 0x1e34e7e 0x20760b4 0x152ba 0xdc4067 0xdc42db 0x14fca 0x16aa7 0x239253f 0x23a4014 0x2393fd6 0x23a4014 0x239b8b7 0x2397405 0x2394768 0x207aaf5 0x2079f44 0x2079e1b 0x2ca87e3 0x2ca8668 0xd78ffc 0x2b8d 0x2ab5)
  libc++abi.dylib: terminate called throwing an exception
  (lldb) 
  </code></pre>
  
  <p>I have tried debugging it to pinpoint exact location or line of code that is causing the crash but with no success. It is just randomly crashing</p>
  
  <p>Thanks<img src="https://i.stack.imgur.com/3NQPH.png" alt="enter image description here"></p>
  
  <p><strong>EDIT 1:</strong></p>
  
  <p>Alright here's the code:</p>
  
  <p><strong>Once fetched I am saving them like:</strong></p>
  
  <pre><code>-(void)saveTweetsArray:(NSMutableArray *)array{
      NSString *dbPath = [DatabaseHelper databasePath];
      sqlite3_stmt *statement;
      if(sqlite3_open([dbPath UTF8String], &amp;db) == SQLITE_OK) {
          NSString *insertSql = [NSString stringWithFormat:@"delete from %@ where ACCOUNTID='%@'",[DatabaseHelper TweetsTable],self.accountId];
          const char *insertStmt = [insertSql UTF8String];
          sqlite3_prepare_v2(db, insertStmt, -1, &amp;statement, NULL);
          if(sqlite3_step(statement) == SQLITE_DONE){
              NSLog(@"old tweets removed");
          }
          else{
              NSLog(@" deletion failed");
              NSLog(@"%@", [NSString stringWithUTF8String:(char*)sqlite3_errmsg(db)]);
          }
          sqlite3_finalize(statement);
          for(int i=0;i&lt;[array count];i++){
              NSMutableDictionary *dictionary = [array objectAtIndex:i];
              NSString *insertSql = [NSString stringWithFormat:@"insert into %@ (ID,TWEET_TEXT,USER_NAME,ACCOUNTID) values (?,?,?,?)",[DatabaseHelper TweetsTable]];
              const char *insertStmt = [insertSql UTF8String];
              sqlite3_prepare_v2(db, insertStmt, -1, &amp;statement, NULL);
              sqlite3_bind_text(statement, 1, [[dictionary valueForKey:@"id"] UTF8String], -1, NULL);
              sqlite3_bind_text(statement, 2, [[dictionary valueForKey:@"text"] UTF8String], -1, NULL);
              sqlite3_bind_text(statement, 3, [[dictionary valueForKey:@"userName"] UTF8String], -1, NULL);
              sqlite3_bind_text(statement, 4, [self.accountId UTF8String], -1, NULL);
              if(sqlite3_step(statement) == SQLITE_DONE)
                  NSLog(@"data saved");
              else{
                  NSLog(@"save failed");
                  NSLog(@"%@", [NSString stringWithUTF8String:(char*)sqlite3_errmsg(db)]);
              }
              sqlite3_finalize(statement);
          }
          sqlite3_close(db);
      }
  </code></pre>
  
  <p><strong>This is where I am fetching to populate on the tableview while new tweets are fetched:</strong></p>
  
  <pre><code>-(NSMutableArray*)tweetsArray{
      NSMutableArray* tweetsArray=[[NSMutableArray alloc] init];
      NSString *dbPath = [DatabaseHelper databasePath];
      if(sqlite3_open([dbPath UTF8String], &amp;db) == SQLITE_OK) {
          const NSString *sql_stmt;
          sql_stmt= [NSString stringWithFormat:@"select ID,TWEET_TEXT,USER_NAME from %@ where ACCOUNTID='%@' ",[DatabaseHelper TweetsTable],self.accountId];
  
              const char *sql_stmt1=[sql_stmt UTF8String];
              sqlite3_stmt *compiledStatement;
              if(sqlite3_prepare_v2(db, sql_stmt1, -1, &amp;compiledStatement, NULL) == SQLITE_OK) {
                  // Loop through the results and add them to the feeds array
                  while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
                      // Read the data from the result row
                      NSMutableDictionary *dictionary=[[NSMutableDictionary alloc] init];
                      [dictionary setValue:[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)] forKey:@"id"];
                      [dictionary setValue:((char *)sqlite3_column_text(compiledStatement, 1))!=NULL?[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)]:@"" forKey:@"text"];
                      [dictionary setValue:[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)] forKey:@"userName"];
                      [textsArray addObject:dictionary];
                  }
                  sqlite3_finalize(compiledStatement);
              }
          }
      sqlite3_close(db);
      return textsArray;
  }
  </code></pre>
  
  <p><strong>This is where I am putting on the tableview after formatting:</strong></p>
  
  <pre><code>        -(void)formatTweetsAndPaintView{
             NSArray *tweets = [dictionary valueForKey:@"twitter"];
  
          //first save the old tweets to show while new are fetched
             NSMutableArray *tempTweets=[[NSMutableArray alloc] initWithArray:[baseController tweetsArray]];
  
          //then clear the old tweets then format and fill array
             [[baseController tweetsArray] removeAllObjects];
  
             if(tweets &amp;&amp; tweets.count){
                     for(int i=0;i&lt;[tweets count];i++){
                             NSMutableDictionary *tweetsDictionary = [[NSMutableDictionary alloc] init];
                             [tweetsDictionary setValue:[[tweets objectAtIndex:i] valueForKey:@"id_str"]    forKey:@"id"];
                             [tweetsDictionary setValue:[[tweets objectAtIndex:i] valueForKey:@"text"] forKey:@"text"];
                             [tweetsDictionary setValue:[[[tweets objectAtIndex:i] valueForKey:@"user"] valueForKey:@"name"] forKey:@"userName"];
  
                             [[baseController tweetsArray] addObject:tweetsDictionary];
                     }
               }
  
  //include non duplicate tempTweet to tweetArray
      for(int i=0;i&lt;[tempTweets count];i++){
                  if(![[baseController tweetsArray] containsObject:[tempTweets objectAtIndex:i]])
                      [[baseController tweetsArray] addObject:[tempTweets objectAtIndex:i]];
              }
  
              if([dictionary valueForKey:@"twitter"])
                  [baseController setDictionary:[dictionary valueForKey:@"twitter"]];
  
              [baseController reloadTableViewData];
  
  //finally save the tweets
              [[TweetDB _instance] saveTweetsArray:[baseController tweetsArray]];
          }
  </code></pre>