Search code examples
iphoneobjective-cnsmutablearrayterminate

iOS NSRangeException, Index Beyond Bounds, NSMutable Array


After inheirting an iPhone app project I'm tasked with the debugging of it after an update. Not an objective c programmer, I'm not sure where to start. Any help would be appreciated.

Here is the debugging output:

2011-07-07 15:27:44.333 App[5836:207] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSMutableArray objectAtIndex:]: index 6 beyond bounds [0 .. 5]'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x013d0be9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x015255c2 objc_exception_throw + 47
    2   CoreFoundation                      0x013c66e5 -[__NSArrayM objectAtIndex:] + 261
    3   CollegeFootballAlerts               0x00034892 -[SMStandings tableView:cellForRowAtIndexPath:] + 397
    ...
)
terminate called after throwing an instance of 'NSException'

the breakpoint is here at teamsInfo = :

- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString* cellIdentifier = @"TeamsInfoCell"; 

    SMStandingsTableViewCell * cell = (SMStandingsTableViewCell *)[tableView1 dequeueReusableCellWithIdentifier:cellIdentifier];
    if( cell == nil ){
        [[NSBundle mainBundle] loadNibNamed:@"SMStandingsTableViewCell" owner:self options:nil];
        cell = standingsTableCell;
        standingsTableCell = nil;
    }
    SMTeamsInfo * teamInfo;
        NSMutableArray * teamsInGroup = [allGroups objectForKey:[allKeys objectAtIndex:indexPath.section]];
        NSLog(@"TEAMS IN GROUP %@ :: %d", teamsInGroup, indexPath.row);
        teamInfo = [teamsInGroup objectAtIndex:indexPath.row];

    [[cell teamName] setText:[teamInfo nameEN]];
    [[cell teamFlag] setImage:[teamInfo teamFlag]];
    NSString * str;
    if([segmentedControl selectedSegmentIndex] == 0) {               // for conference tab wonconf - lostconf  combination is used 
        str= [NSString stringWithFormat:@"%@",[teamInfo wonconf]];
        str = [str stringByAppendingString:@"-"];
        str = [str stringByAppendingFormat:@"%@",[teamInfo lostconf]];
    }else {                                                          // for overall tab wonconf - lostconf  combination is used 
        str= [NSString stringWithFormat:@"%@",[teamInfo wonall]];
        str = [str stringByAppendingString:@"-"];
        str = [str stringByAppendingFormat:@"%@",[teamInfo lostall]];
    }

    [[cell currentStanding ]setText:str];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;    
}

and the numberOfRowsInSection method:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray * array = [allGroups allKeys];
    NSLog(@"NO OF ROWS IN SECTION #%d : %d", section, [[allGroups objectForKey:[array objectAtIndex:section]] count]);  
    return [[allGroups objectForKey:[array objectAtIndex:section]] count];
}

and the output of that method:

2011-07-08 17:46:13.837 App[7513:207] NO OF ROWS IN SECTION #1 : 7
2011-07-08 17:46:13.837 App[7513:207] NO OF ROWS IN SECTION #0 : 6

not sure why section #1 coming in first... the number of rows is reverse. it should be 7 for section 0 and 6 for section 1 which seems like the root of the problem.


Solution

  • did you get this in xcode while debugging? if so it should auto take you to the line it happens on.

    If not, it is happening from the looks of things somewhere in SMStanding's tableView: cellForRowAtIndexPath: method. and looks like a generic array out of bounds issue. If you need help with that, edit your question with the code from that method.

    Edit: In general, for this kind of stack trace, I try to find a class name/method that is something that I made and go from there.