Error: index 0 beyond bounds for empty array


#1

Getting this error:

[quote]2012-07-15 15:21:21.426 Homepwner[880:f803] *** Terminating app due to uncaught exception ‘NSRangeException’, reason: ‘*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array’
*** First throw call stack:[/quote]

code:

[code]- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
BNRItem *p = [[[BNRItemStore sharedStore] allItems] objectAtIndex:[indexPath row]];

// get the new or recycled cell
HomepwnerItemCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HomepwnerItemCell"];

// configure the cell with the BNRItem
[[cell nameLabel] setText:[p itemName]];
[[cell serialNumberLabel] setText:[p serialNumber]];
[[cell valueLabel] setText:[NSString stringWithFormat:@"$%d", [p valueInDollars]]];  

return cell;

}[/code]

Can anyone help? Thanks.


#2

You are trying to get the first element from an array which is empty.

Make sure that value of your index is within bounds:

   if ([indexPath row] >= [[[BNRItemStore sharedStore] allItems] count])
   {
        return nil;
   }
   BNRItem *p = [[[BNRItemStore sharedStore] allItems] objectAtIndex:[indexPath row]];
   ...

#3

Thanks! worked.