Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to push to final view where the whole information about item will be shown.

- (void)tableView:(UITableView *)atableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Item *itemObj; //object that holds data for cell for current view
ItemShow *itemOverViewObj; //data object for the next view 

if(atableView==self.tableView)
{
    //ordinary tabeView
    itemObj=[self.itemsArray objectAtIndex:[indexPath row]];
}
else
{
    //filtered with search tableView
    itemObj=[self.filteredItems objectAtIndex:[indexPath row]];
}

//The array which will store the data for overview 
NSMutableArray *itemsOverViewArr=[[NSMutableArray alloc]initWithCapacity:1];
DBAccess *access=[[DBAccess alloc]init];

//method for fetching data from db and inserting into array
itemsOverViewArr=[access getItemsOverView:itemObj.itemID];
[access closeDataBase];
[access release];

//here is the evil.
itemOverViewObj=[itemsOverViewArr objectAtIndex:[indexPath row]];
[itemsOverViewArr release];
    ItemOverView *iov=[[ItemOverView alloc]initWithNibName:@"ItemOverView" bundle:nil];
    iov.title=self.nominal;
    [iov setItemShow:itemOverViewObj];
    [self.navigationController pushViewController:iov animated:YES];
    [iov release];
}

What I've checked before:

Select in the method getItemsOverView works for 100%. itemObj.itemID fetches the right int.

The problem originating in itemOverViewObj=[itemsOverViewArr objectAtIndex:[indexPath row]];

The error: * Terminating app due to uncaught exception 'NSRangeException', reason: '* -[NSMutableArray objectAtIndex:]: index 1 beyond bounds [0 .. 0]'

getItemsOverview meth in DBAccess Class

-(NSMutableArray*)getItemsOverView:(int)itemID
{
NSMutableArray *itemsArray=[[[NSMutableArray alloc]init]autorelease];
const char *sqlItems=sqlite3_mprintf("SELECT itm.itemID,itm.itemYear,itm.rarity,dateComment,itm.mintage,dc.dateCode as dateCode,iaval.availability as avalibility,iaval.quality as quality,itm.Mintmark,itm.specialRemark
                                     from items itm
                                     join itemAvailability iaval on iaval.itemID=itm.itemID
                                     join dateCultures dc ON dc.dateCultureID=itm.dateCulture
                                     WHERE itm.itemID=%i",itemID);
sqlite3_stmt *statement;
int sqlResult = sqlite3_prepare_v2(database, sqlItems, -1, &statement, NULL);
if ( sqlResult== SQLITE_OK)
{
    while (sqlite3_step(statement) == SQLITE_ROW)
    {
        ItemShow *itemShow=[[ItemShow alloc]init];
        itemShow.itemID=sqlite3_column_int(statement, 0);
        char *itemYear=(char *)sqlite3_column_text(statement, 1);
        char *mintMark=(char *)sqlite3_column_text(statement, 2);
        char *masterMark=(char *)sqlite3_column_text(statement, 3);
        itemShow.rarity=sqlite3_column_int(statement, 4);
        itemShow.availability=sqlite3_column_int(statement, 5);
        itemShow.quality=sqlite3_column_int(statement, 6);
        char *mintage=(char *)sqlite3_column_text(statement, 7);

        itemShow.itemYear=(itemYear)?[NSString stringWithUTF8String:itemYear]:@"";
        itemShow.mintMark=(mintMark)?[NSString stringWithUTF8String:mintMark]:@"";
        itemShow.masterMark=(masterMark)?[NSString stringWithUTF8String:masterMark]:@"";
        itemShow.mintage=(mintage)?[NSString stringWithUTF8String:mintage]:@"Unknown";



        [itemsArray addObject:itemShow];
        [itemShow release];

    }
    sqlite3_finalize(statement);
}
else
{
    [self dbConnectionError];
}

return itemsArray;
}

Thank you in advance

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
180 views
Welcome To Ask or Share your Answers For Others

1 Answer

The exception message says it all: You are trying to access the second item (index 1) in itemsOverViewArr but this array only contains one item (index 0).

Why that is so only you can tell since you don't show us the relevant code. But it sure looks like [access getItemsOverView:itemObj.itemID] returns an array containing 1 element.

Also, you have at least one memory leak in your code. The array created by NSMutableArray *itemsOverViewArr=[[NSMutableArray alloc]initWithCapacity:1]; is no longer reachable after the line itemsOverViewArr=[access getItemsOverView:itemObj.itemID]; and thus cannot be released. The fact that you are releasing itemsOverViewArr later is probably also a memory management error (in this case an overrelease) because getItemsOverView: should return an autoreleased object. (Btw, you should not name such methods get... as this naming convention implies in Cocoa that the method returns its results via a pointer to one of the method's arguments.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...