2012-07-02 88 views
5

我收到此錯誤:[NSMutableArray objectAtIndex:]: index 0 beyond bounds for empty array雖然我有75個項目在我的數據庫中。爲什麼它向我顯示這個錯誤?我想從我的數據庫中的項目,但它告訴我,指數爲0。[NSMutableArray objectAtIndex:]:索引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; 

}

+0

檢查arrAlert是否爲空? –

+0

check [arrItemsList Count]; – Deepesh

回答

2

如果你有信心,你的DB調用應該返回的數據,我將確保至少一個你的if()else()語句實際上正在被擊中。否則,你的數組爲空給你初始化它上面:

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

一個簡單的方法來檢查自己將在您的ifelse鏈的底部添加一個else聲明,並斷言的東西在那裏(即至少打印一些東西,對於初學者)。

+0

好的。實際上這個應用程序運行良好,突然得到這個錯誤... – user1482192

+0

雅其他部分工作正常... – user1482192

相關問題