[NSMutableArray objectAtIndex:]:索引0超出空数组的边界'

时间:2012-07-02 06:57:26

标签: objective-c nsmutablearray

我收到此错误:[NSMutableArray objectAtIndex:]: index 0 beyond bounds for empty array虽然我的数据库中有75个项目。为什么它会向我显示此错误?我正在尝试从我的数据库中获取项目,但它告诉我索引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 个答案:

答案 0 :(得分:2)

如果您确信您的数据库调用应该返回数据,我会确保至少有一个if()else()语句实际上被命中。否则,如果你在上面初始化它,你的数组是空的:

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

检查自己的简单方法是在else if链的底部添加else语句,并在那里断言(即至少打印一些内容,起动器。)