1. Welcome to the new MacRumors forums. See our announcement and read our FAQ


Discussion in 'iPhone/iPad Programming' started by cb1, Jul 16, 2010.

  1. cb1
    macrumors newbie

    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?
  2. macrumors 6502a

    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.
  3. cb1
    macrumors newbie

    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?
  4. Moderator


    Staff Member

    Use the debugger: it will show you the exact line of code that it fails on.
  5. macrumors 68030


    Check this menu item: Run > Stop on Objective-C Exceptions. Then run the app in the debugger.
  6. cb1
    macrumors newbie

    the debugger stops at "objc_exception_throw". can someone tell me what this means?
  7. Moderator


    Staff Member

    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
  8. cb1
    macrumors newbie

    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.
  9. macrumors 68030


    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.

Share This Page