The application terminated due to a nullable exception "NSRangeException", reason: "*** - [__ NSArrayM objectAtIndex:]: index 0 outside for empty array '

I develop applications using web services. I got this error, as it happened when I tested real devices, but when I run the Retina 3.5 simulator it works fine, please help me, I tried, but I can not fix it.

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 

The 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; } 
-2
json ios objective-c
source share
3 answers

To understand the concept of accessing an array object, here I gave an example that explains why it crashes when accessing an object by index in the array.

I am not going to indicate where you made mistakes. understand the concept and based on this change the code that you like [yourArray objectAtIndex:10] .

Nonprofit example:

You have a bag (NSMutableArray) on which there are 10 balls marked (0,1,2 ... 9), but how do you recognize the 11th ball. Impossible !.

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

2. Then try to access it.

3. Your problem is here, you are looking for the first ball (0th marked) 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 \n NSMutableArray *array = [[NSMutableArray alloc]init]"); } 
+1
source share

You can try:

 id myObject = [myArray firstObject]; if(nil != myObject) { // Do something } 

But without showing us the code, I can no longer offer.

0
source share

You can do the trick with the number of delegates of rows.If loadcell array is empty. Then the cell will load in blank lines.

 loadingcell=[[NSMutableArray alloc]init];//check that you initialized and allocated loadingcell 

Change your code as shown below.

  - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return loadingcell.count; } -(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] ; } if(loadingcell.count>0) { 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; } 

Hope this helps you.

0
source share

All Articles