3

I am getting the following error if the @"Comments" array is 0 and the other two are 1 or greater.

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

This is populating the self.items array.

How can I account for the fact that some of the self.notification (Comments in this case) arrays may be null and still have my code run without throwing this type of error?

NSDictionary *commentsDict = [NSDictionary dictionaryWithObject:self.notification.Comments forKey:@"Comments"];
NSDictionary *likesDict = [NSDictionary dictionaryWithObject:self.notification.Likes forKey:@"Likes"];
NSDictionary *friendsDict = [NSDictionary dictionaryWithObject:self.notification.Likes forKey:@"Friends"];

self.items = [NSMutableArray array];

[self.items addObject:commentsDict];
[self.items addObject:likesDict];
[self.items addObject:friendsDict];

This is the code where it fails:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSDictionary *dictionary = [self.items objectAtIndex:section];
    NSArray *array = nil;

    if (section == 0) {
        array = [dictionary objectForKey:@"Comments"]; // 0 count
    } else if (section == 1) {
        array = [dictionary objectForKey:@"Likes"]; // 1 count or greater
    } else {
        array = [dictionary objectForKey:@"Friends"]; // 1 count or greater
    }

    return [array count];
}
gotnull
  • 26,454
  • 22
  • 137
  • 203
  • Can you tell use more about your data structure? What really contains items? Shouldn't you simply return [[self.items objectAtIndex:section] count] ? – j_freyre Mar 02 '11 at 09:19
  • can you post the exception message? as the real exception is somewhat difficult message, but may it contain any hint or useful information to identify the error – Waqas Raja Mar 02 '11 at 09:50
  • 1
    You having empty array.possibly your array got released. – Ishu Mar 02 '11 at 09:51
  • Updated original question code. – gotnull Mar 02 '11 at 09:54

5 Answers5

2

put a NSLog(@"Array: %@", self.items); in front of your code and you will see that the array is indeed empty.

Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
1

You're trying to get an object by an index that is not there. Perhaps the self.items array doesn't have an object for each section index. Fromt the error message it looks like self.items is actually empty (zero objects).

Rengers
  • 14,911
  • 1
  • 36
  • 54
1

Maybe you have too many sections defined (more than the number of items in self.items):

Then this would fail:

[self.items objectAtIndex:section]
Philippe Leybaert
  • 168,566
  • 31
  • 210
  • 223
1

To work this out, you'll need to look at how you set self.items and how you implemented - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView.

Either self.items is releasing the array early or how you're returning the number of sections is independent of self.items.

benwong
  • 2,226
  • 1
  • 16
  • 14
0

This was an issue with my - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section; as I was only referencing the Comments array instead of the other two when changing the section header.

gotnull
  • 26,454
  • 22
  • 137
  • 203