NSRangeException

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

  1. cb1
    macrumors newbie

    Joined:
    Jun 30, 2010
    #1
    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?
     
  2. macrumors 6502a

    Joined:
    Mar 19, 2008
    Location:
    North Shore, MA
    #2
    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
    thread starter macrumors newbie

    Joined:
    Jun 30, 2010
    #3
    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?
     
  4. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    Use the debugger: it will show you the exact line of code that it fails on.
     
  5. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #5
    Check this menu item: Run > Stop on Objective-C Exceptions. Then run the app in the debugger.
     
  6. cb1
    thread starter macrumors newbie

    Joined:
    Jun 30, 2010
    #6
    the debugger stops at "objc_exception_throw". can someone tell me what this means?
     
  7. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #7
    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
    thread starter macrumors newbie

    Joined:
    Jun 30, 2010
    #8
    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.
     
  9. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #9
    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.
     

Share This Page