5

I'm getting this error: [NSMutableArray objectAtIndex:]: index 0 beyond bounds for empty array although I have 75 items in my database. Why is it showing me this error? I'm trying to get items from my database but it tells me index 0.

+(NSMutableArray *)GetAttitudeItems
{
    NSArray *arrDocPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *strDestPath = [NSString stringWithFormat:@"%@/ItemsAndCategory.sqlite",[arrDocPath objectAtIndex:0]];


//sqlite3 *sqlite;

NSMutableArray *arrItemsList;
if(sqlite3_open([strDestPath UTF8String],&database)==SQLITE_OK)
{

    const char *strQuery = "SELECT * FROM Items WHERE CategoryName='ATTITUDE' ORDER BY RANDOM()";
    NSLog(@"The value is %s",strQuery);
    sqlite3_stmt *stmt;
    //char *err;

    arrItemsList  = [[NSMutableArray alloc]init];

    if(sqlite3_prepare_v2(database, strQuery , -1,&stmt, NULL)==SQLITE_OK)
    {
        while (sqlite3_step(stmt)==SQLITE_ROW) 
        {

            NSString *StrCatename = [NSString stringWithUTF8String:(char*)sqlite3_column_text(stmt, 1)];
            NSString *StrItemName = [NSString stringWithUTF8String:(char*)sqlite3_column_text(stmt, 0)];

            Itemslist *itemlist = [[Itemslist alloc]init];
            itemlist.CategoryValue = StrCatename;
            itemlist.ItemValue =StrItemName;
            [arrItemsList addObject:itemlist];

        }

    }

    if (stmt!=nil)
    {
        sqlite3_finalize(stmt);
    }
}



sqlite3_close(database);

return arrItemsList;

}

1
  • Check arrAlert is empty or not? Commented Jul 2, 2012 at 7:04

1 Answer 1

2

If you are confident that your DB calls should return data, I would make sure that at least one of your if () else () statements is actually getting hit. Otherwise, your array is empty given that you initialized it above:

NSMutableArray *arrAlert = [[NSMutableArray alloc]init];

A simple way to check for yourself would be to add an else statement at the bottom of your if else chain, and assert something there (i.e. at least print something, for starters.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.