I know I have a bug somewhere… I a very simple bug indeed, it’s just an index out of bounds error. I used to hunt and place breakpoints in my project to see where it is. But today I decided to be smart and to actually learn to read the error message, because I know it’s all there! But I don’t know how to read it, I tried googling and none seems to give me a good explanation!
Can anyone help me read this error message?
2011-02-01 00:28:04.952 FBDiary[44083:207] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSMutableArray objectAtIndex:]: index 26 beyond bounds [0 .. 25]'
*** Call stack at first throw:
(
0 CoreFoundation 0x011dabe9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x0132f5c2 objc_exception_throw + 47
2 CoreFoundation 0x011d06e5 -[__NSArrayM objectAtIndex:] + 261
3 FBDiary 0x00007035 -[FBFriendsList tableView:heightForRowAtIndexPath:] + 101
4 UIKit 0x004a3334 -[UISectionRowData refreshWithSection:tableView:tableViewRowData:] + 2888
5 UIKit 0x004a1e8b -[UITableViewRowData(UITableViewRowDataPrivate) _ensureSectionOffsetIsValidForSection:] + 152
6 UIKit 0x004a0a0c -[UITableViewRowData numberOfRows] + 145
7 UIKit 0x003578c2 -[UITableView noteNumberOfRowsChanged] + 132
8 UIKit 0x003642b8 -[UITableView reloadData] + 773
9 UIKit 0x04e5d1f0 -[UITableViewAccessibility(Accessibility) reloadData] + 60
EDIT: So a few minutes after posting the question I found the bug… As you said it’s in the tableView:heightForRowAtIndexPath: function. However what I really wanted to know is, can I infer the line where it’s causing problem from this stack trace?
there’s not much to read. and as usual the stacktrace alone won’t help you to fix the bug.
For this bug you need understanding of the UITableViewDatasource protocol.
the error is caused by
-[FBFriendsList tableView:heightForRowAtIndexPath:], where you most likely try to access an element of the data array because you want to return the corresponding height.So combine your knowledge about UITableViewDatasource with your exception.
Your knowledge about the datasource should tell you that a tableview asks for heightForRowAtIndexPath for all cells currently visible.
It should also tell you that you’ve told the tableview how many rows it should access.
If you combine this knowledge and your exception you should come to the conclusion that the value you return in
- (NSInteger)tableView:(UITableView *)tableView_ numberOfRowsInSection:(NSInteger)sectionis wrong, and most likely it’s 26 where it should be 25.This is the most common bug that happens with UITableView.