Q: Exception when trying to populate table view controller with the data retrieved from sql + NSRangeException' I am new to iOS development and i face weird problem (cuz the same code in both table view controller and sql class works pretty good with other entity).
I am trying to retrive the data from sqlite database to my table view controller..
and this error shows up when i try to insert more than one record in the database.
2012-11-10 19:04:20.577 my app[7032:11303] * Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
* First throw call stack:
Any help will be appreciated.
Thank you.

import <UIKit/UIKit.h>
import "Category.h"
import "DbOperations.h"
import "AddCategoriesTVC.h"
import "CategoryDetailTVC.h"@interface CategoriesTVC : UITableViewController <UITableViewDataSource, UITableViewDelegate,AddCategoriesTVCDelegate, CategoryDetailTVCDelegate>

@property (strong, nonatomic) Category *categoryItem;
@property(nonatomic, strong) DbOperations * ops;
@property(nonatomic, strong) NSMutableArray * myCategoryArray;
@property (strong, nonatomic) IBOutlet UITableView * categoryTable;
@end
///////////
import "CategoriesTVC.h"
import "Category.h"
import "DbOperations.h"
import "sqlite3.h"
@implementation CategoriesTVC
@synthesize categoryTable;
@synthesize categoryItem,myCategoryArray,ops;

- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
    // Custom initialization
}
return self;
}

- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{ops = [[DbOperations alloc]init];
[ops selectCategories];
myCategoryArray = [ops categoryArray];
self.editButtonItem.title = @"edit";
self.navigationItem.leftBarButtonItem = self.editButtonItem;
[super viewDidLoad];
}


- (void)viewDidUnload
{
[self setCategoryTable:nil];
[super viewDidUnload];
}

- (void)viewWillAppear:(BOOL)animated
{[super viewWillAppear:animated];
//myCategoryArray =[[NSMutableArray alloc]init];//]
[ops selectCategories];
myCategoryArray = [ops categoryArray];
[categoryTable reloadData];}

//pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
// Return the number of sections.
return 1;}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
 NSLog(@"JJJJ2");
// Return the number of rows in the section.
return [self.myCategoryArray count];}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Category Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

Category * categoryObj = [ops.categoryArray objectAtIndex:indexPath.row];
cell.textLabel.text = categoryObj.categoryName;
cell.textLabel.textAlignment = NSTextAlignmentCenter;//UITextAlignmentCenter;

return cell;}


@interface DbOperations : NSObject
{
sqlite3 * db;
}
//to add the contents of the database which will be the data source for the table
@property(nonatomic,strong)NSMutableArray * categoryArray;

//instance methods for ** Category Entity **

-(void) selectCategories;
-(void) insertCategory:(NSString *)categoryName;
-(void) updateCategory:(NSString *)categoryName:(NSString *)categoryName2;
-(void) deleteCategory:(NSString *)categoryName;
------------------------------------------
------------------------------------------
-(void) selectCategories{

categoryArray = [[NSMutableArray alloc] init];
//Get list of directories in Document path
NSArray * dirPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

//Define new path for database
NSString * documentPath = [[dirPath objectAtIndex:0] stringByAppendingPathComponent:@"SmartSpeakerDB.sqlite"];
//By default there should only have one item in the array, so the index is always 0 unless you add subdirectories.

if(!(sqlite3_open([documentPath UTF8String], &db) == SQLITE_OK))
{
    NSLog(@"An error has occured.");

    return;
}else{//e2


    const char *sql = "SELECT categoryName FROM Category";
    //const char * sql, will be execute the SELECT statement.
    sqlite3_stmt *sqlStatement;


    if(sqlite3_prepare_v2(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK)
    {
        NSLog(@"There is a problem with prepare statement Hah");
        return;
    }else{
        while (sqlite3_step(sqlStatement)==SQLITE_ROW) {
             NSLog (@"ENTER while");

 char * checkChar = (char*)sqlite3_column_text(sqlStatement,0);//1
    if (checkChar!= NULL) {
        NSString *s=[NSString stringWithUTF8String:checkChar];
                Category * newCategory = [[Category alloc] init];
        newCategory.categoryName = s;

             NSLog (@"***");
                [categoryArray addObject:newCategory];
            NSLog(@"Name %@",newCategory.categoryName);
               newCategory = nil;

     }//END IF
        }//END While
        sqlite3_finalize(sqlStatement);
        sqlite3_close(db);
    }//END ELSE 1
}//END ELSE 2
}//End


A: I looks like it is probably because you are using:
[self.myCategoryArray count];

in numberOfRowsInSection but:
[ops.categoryArray objectAtIndex:indexPath.row];

in cellForRowAtIndexPath.
If the two arrays have different numbers of objects, then you will have this problem.  You typically use the same array for both functions so that this can't happen.

A: You don't state where in all of that code you are actually having the problem but I see one likely issue. In your tableView:numberOfRowsInSection: method you return:
[self.myCategoryArray count];

But then in your tableView:cellForRowAtIndexPath: method you call:
Category * categoryObj = [ops.categoryArray objectAtIndex:indexPath.row];

You are using two different arrays here. These should be based on the same data.
For future reference, when posting these type of questions, point out the exact line the exception is coming from and only post relevant code.
