4

I'm working on someone else's app and and I'm getting this error:

'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 0 
 beyond bounds for empty array'

Xcode points to this line:

 NSDictionary *geo = [response[0] objectForKey:@"geometry"];

Here's the full method:

- (void)loadLatLong {

indx ++;
if (indx == [datalist count]) {
    [self showMarker];
//        self.alertView.hidden = true;
//        [self setThreeQuarters];
    [self.progressbar setProgress:1.0 animated:YES];
    return;
}
PointClass *pt = datalist[indx];
NSString *stPt = [pt.Location stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *str = [stPt stringByReplacingOccurrencesOfString:@" " withString:@"+"];
NSString *url = [NSString stringWithFormat:@"https://maps.google.com/maps/api/geocode/json?address=%@&sensor=false&key=AIzaSyAoSBH8mBmeVp2HVx6vJrkTvl3bjRFiWag",str];

NSURL *urlString = [NSURL URLWithString:url];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
[manager GET:[urlString absoluteString ]parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSArray *response = [responseObject objectForKey:@"results"];
    NSDictionary *geo = [response[0] objectForKey:@"geometry"];
    NSDictionary *location = [geo objectForKey:@"location"];

    pt.lat = [[location objectForKey:@"lat"] doubleValue];
    pt.lng = [[location objectForKey:@"lng"] doubleValue];
    datalist[indx] = pt;
    NSLog([NSString stringWithFormat:@"%%d \n"], indx);
    [self loadLatLong];

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

  }];
}

Can't figure out where to change the code.

UPDATE: I want to add that it only crashes sometimes and sometimes it loads

2 Answers 2

9

The code makes an assumption that response array always has at least one element. This breaks when the array is empty, so you need to add code to check for it:

NSArray *response = [responseObject objectForKey:@"results"];
if (response.count == 0) {
    // Continue loading more data:
    [self loadLatLong];
    return;
}
NSDictionary *geo = [response[0] objectForKey:@"geometry"];
Sign up to request clarification or add additional context in comments.

15 Comments

@dasblinkenlight Well, the app doesn't crash, but stops downloading objects at number 71. I think there are 85 objects in the array. screencast.com/t/i1bMk3EW
@Paul Create a new question since there is now a new problem.
@dasblinkenlight OK, I see. It stops on 71 because there isn't an element.It's zero, everything else is 1. I think it's downloading this from the Google server. screencast.com/t/kYr9OU0S2
@dasblinkenlight Is this a server issue?
@dasblinkenlight The app stopped crashing, but stops on record 70. Is this a sever problem? I checked the server and everything is populated.
|
1

It is crashed because the response don't have any object. You should check it before calling response[0]. Something like this if ([response count] == 0) return;

1 Comment

The app just stops downloading objects at number 71 after putting in your code. I think there are 85 objects in the array. screencast.com/t/i1bMk3EW

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.