PDA

View Full Version : NSRangeException




cb1
Jul 16, 2010, 05:13 AM
Can someone explain with this error message please?


Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFArray objectAtIndex:]: index (2) beyond bounds (1)'


It seems to be caused by this piece of code:


NSMutableArray *wholeArray = [[NSMutableArray alloc] init];

NSEnumerator *enumerator = [linesArray objectEnumerator];// linesArray is an array containing several long strings
id currentObject;
while (currentObject = [enumerator nextObject]) {
NSArray *array = [currentObject componentsSeparatedByString:@","];
Entry *line = [[Entry alloc] initWithdate:[array objectAtIndex:0] time:[array objectAtIndex:1] value:[array objectAtIndex:2]]; /*Entry is just a custom class which takes in three strings when initialized.*/

[wholeArray addObject:line];

}

Does anyone know what the problem is?



Guiyon
Jul 16, 2010, 06:23 AM
Exactly what the exception says: you are accessing an index in your array that doesn't exist. More specifically, you are attempting to access the 3rd element (index of 2) in an array that only contains 2 elements.

cb1
Jul 16, 2010, 06:59 AM
ok i understand that but where exactly is the code trying to access this non-existant index. i first assumed it was to do with [array objectAtIndex:2]

but i added the following code to test this:

NSString *test =[array objectAtIndex:2];

NSLog(@"%@", test);

This printed off the strings from this array correctly verifying that there are 3 elements. so where is the mistake?

robbieduncan
Jul 16, 2010, 07:47 AM
Use the debugger: it will show you the exact line of code that it fails on.

PhoneyDeveloper
Jul 16, 2010, 07:53 AM
Check this menu item: Run > Stop on Objective-C Exceptions. Then run the app in the debugger.

cb1
Jul 16, 2010, 08:36 AM
the debugger stops at "objc_exception_throw". can someone tell me what this means?

robbieduncan
Jul 16, 2010, 08:39 AM
That's what we'd expect: you have told it to stop on exception throwing and it stops on the throw exception function. You need to look down the call stack to find the line in your code that caused this to be thrown.

Given that you seem to be struggling to understand the debugger I suggest you read the documentation on it so you know what you are doing.

Edit to add: This is the document to read (http://developer.apple.com/iphone/library/documentation/DeveloperTools/Conceptual/XcodeDebugging/000-Introduction/Introduction.html)

cb1
Jul 16, 2010, 08:55 AM
it seems to say the problem is with this line:


Entry *line = [[Entry alloc] initWithdate:[array objectAtIndex:0] time:[array objectAtIndex:1] value:[array objectAtIndex:2]];

i still don't see it though.

PhoneyDeveloper
Jul 16, 2010, 08:59 AM
Break the line down into separate lines. Your compound statements are difficult to debug.

bla = [array objectAtIndex:0];
blabla = etc.

You should be able to use the debugger to inspect the values of the array and each of these single line values.