Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

cb1

macrumors newbie
Original poster
Jun 30, 2010
9
0
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:
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?
 
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.
 
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:
Code:
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?
 
the debugger stops at "objc_exception_throw". can someone tell me what this means?
 
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
 
it seems to say the problem is with this line:


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

i still don't see it though.
 
Break the line down into separate lines. Your compound statements are difficult to debug.

Code:
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.