Parth Mody
Parth Mody

Reputation: 466

NSRangeException beyond bounds after calling JSON

I am calling Google's geocoding API and the Distance Matrix API on an asynchronous thread in my app.But its giving out an NSRangeException. Here's my code:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^
{
    NSString *urlStringGeoCoding;
    NSString *urlString;

    urlStringGeoCoding = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/"
                                                    @"api/geocode/json?"
                                                    @"latlng=40.714224,-73.961452&sensor=true"];

    urlString = [NSString stringWithFormat:@"http://maps.googleapis.com"
                                           @"/maps/api/distancematrix/json?"
                                           @"origins=%@&destinations=%@&mode=driving&"
                                           @"language=en&sensor=true&units=imperial","
                                           @"current,@"Chicago,IL"];

    NSURL   *urlGeo = [NSURL URLWithString:[urlStringGeoCoding
                          stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSURL   *url    = [NSURL URLWithString:[urlString
                          stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSData  *googledatageo = [NSData dataWithContentsOfURL:urlGeo];
    NSData  *googledata    = [NSData dataWithContentsOfURL:url];
    NSError *errorgeo;
    NSError *error;

    NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:googledata 
                                                                options:kNilOptions 
                                                                  error:&error];
    NSMutableDictionary *jsongeo = [NSJSONSerialization JSONObjectWithData:googledatageo 
                                                                   options:kNilOptions 
                                                                     error:&errorgeo];

    NSString *result = [[[[[[json objectForKey:@"rows"] objectAtIndex: 0] objectForKey:@"elements"] objectAtIndex:0]objectForKey:@"distance"]objectForKey:@"text"];
    if(jsongeo)
    {
       NSString *resultgeo = [[[jsongeo objectForKey:@"results"] objectAtIndex: 0] objectForKey:@"formatted_address"] ;
    }

    dispatch_sync(dispatch_get_main_queue(), ^
    {     
        cell.distanceLabel.text = result;
    });
});

Here's the error I am getting: Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayI objectAtIndex:]: index 0 beyond bounds for empty array' * First throw call stack: (0x289a012 0x2210e7e 0x284fb44 0x1318c 0x57f953f 0x580b014 0x57fc2e8 0x57fc450 0x9112ae72 0x91112d2a)

How do I prevent the Range Exception here?

Upvotes: 1

Views: 266

Answers (1)

meaning-matters
meaning-matters

Reputation: 22946

You prevent these range exceptions by checking the count of the "rows", "elements", and "results" arrays, before calling objectAtIndex: on them.

It was a good idea to break up these two very long lines that assign result and resultgeo anyway; just use a few temporary NSArray variables.

Upvotes: 1

Related Questions