Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
186 views
in Technique[技术] by (71.8m points)

ios - Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'

I'm developing apps using web services I got this error like this it was happen when I test the real devices but when I run the retina 3.5 simulator it works perfectly please help me I have tried but I cannot fix this.

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
*** First throw call stack:
(0x30b59fd3 0x3b308ccf 0x30a9081b 0xeec19 0x334a8917 0x3344fc47 0x3344f49d 0x33375d79 0x32ff362b 0x32feee3b 0x32feeccd 0x32fee6df 0x32fee4ef 0x33379401 0x30b2525b 0x30b2472b 0x30b22f1f 0x30a8df0f 0x30a8dcf3 0x35992663 0x333d916d 0x100ec1 0x3b815ab7)
libc++abi.dylib: terminating with uncaught exception of type NSException

Code :

-(void)loaddetails
{
    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://api.espn.com/v1/sports/football/news/headlines?apikey=th98vzm67pufc36ka42xxxmy"]];
    [request setHTTPMethod:@"GET"];
    [request setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"content-type"];
    NSError *err;
    NSURLResponse *response;
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
    NSDictionary *jsonArray=[NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&err];

    NSArray *headlinetop=[jsonArray objectForKey:@"headlines"];

    for (int i=0; i<[headlinetop count]; i++)
    {
        NSString *headstr=[[headlinetop objectAtIndex:i]objectForKey:@"headline"];
        [loadingcell addObject:headstr];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;

    }
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(10, 10.0, 250, 26.0)];
    title.backgroundColor = [UIColor clearColor];
    title.textAlignment = NSTextAlignmentLeft;
    title.textColor=[UIColor blackColor];
    title.font = [UIFont fontWithName:@"Arial Hebrew" size:15.0];
    title.text=[loadingcell objectAtIndex:indexPath.row];
    [cell.contentView addSubview:title];
    if ([[UIDevice currentDevice]userInterfaceIdiom]==UIUserInterfaceIdiomPhone) {
        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    }


    return cell;
}
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

For understanding the concept of accessing the array object, here i came up with the example which explains clearly why its crashing when you access the object at index in array.

I'm not going to pointout where you did mistakes. understand the concept and based on that change the code whereever you have like [yourArray objectAtIndex:10].

Non Software example:

You have bag(NSMutableArray) which has 10 balls labeled from (0,1,2...9), but how would you find out the 11th ball. Not Possible!.

1.First you should have bag in your hand readily.([[NSMutableArray alloc] init])

2.Then try to access it.

3.Your problem here is you are looking for first ball(0th labeled) inside the bag . iOS says "Sorry"

//assume [array count] == 10 (0,1,2,3,4,5,...9)

NSUInteger indexTobeAccessedInArray = 5;//6th object (0,1,2,3,4,5,...9)


if (array && [array count]>0) {
    // array lives in memory and it has atleast one objects in it

    // 5 < 10
    if (indexTobeAccessedInArray < [array count]) {
        //Yes
        NSLog(@"you can access this index:%i",indexTobeAccessedInArray);

    }
    else{
        //No
        NSLog(@"Index %i beyond bounds for array",indexTobeAccessedInArray);

    }

}
else{
    // There was no array in memory
    NSLog(@"sorry there is no array, you should create & initialize it first like 
 NSMutableArray *array = [[NSMutableArray alloc]init]");

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...