8

I don't understand on how to debug this error message:

2011-02-01 20:45:56.151 NeMe[3206:207] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSMutableArray objectAtIndex:]: index 0 beyond bounds for empty array'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x027deb99 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x0292e40e objc_exception_throw + 47
    2   CoreFoundation                      0x027d4695 -[__NSArrayM objectAtIndex:] + 261
    3   NeighborMe                          0x0000f617 -[NeighborMapViewController regionFromLocations] + 65
    4   NeighborMe                          0x0001047b -[NeighborMapViewController locationManager:didUpdateToLocation:fromLocation:] + 94
    5   CoreLocation                        0x02393870 -[CLLocationManager onClientEventLocation:] + 793
    6   CoreLocation                        0x0239218b OnClientEvent + 49
    7   CoreLocation                        0x023a8a83 _Z22CLClientInvokeCallbackP10__CLClient13CLClientEventPK14__CFDictionary + 47
    8   CoreLocation                        0x023a9b2c _Z27CLClientHandleDaemonDataFixP10__CLClientPK23CLDaemonCommToClientFixPK14__CFDictionary + 290
    9   CoreLocation                        0x023acb30 _Z24CLClientHandleDaemonDataP12__CFMachPortPvlS1_ + 1125
    10  CoreFoundation                      0x02720982 __CFMachPortPerform + 338
    11  CoreFoundation                      0x027bfff4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
    12  CoreFoundation                      0x02720807 __CFRunLoopDoSource1 + 215
    13  CoreFoundation                      0x0271da93 __CFRunLoopRun + 979
    14  CoreFoundation                      0x0271d350 CFRunLoopRunSpecific + 208
    15  CoreFoundation                      0x0271d271 CFRunLoopRunInMode + 97
    16  GraphicsServices                    0x030bd00c GSEventRunModal + 217
    17  GraphicsServices                    0x030bd0d1 GSEventRun + 115
    18  UIKit                               0x002eeaf2 UIApplicationMain + 1160
    19  NeighborMe                          0x00002818 main + 102
    20  NeighborMe                          0x000027a9 start + 53
    21  ???                                 0x00000001 0x0 + 1
)
terminate called after throwing an instance of 'NSException'

There is NSMutableArray in this code... so how is this possible?

aherlambang
  • 14,290
  • 50
  • 150
  • 253
  • 7
    Simple: your `NSMutableArray` is empty and you are trying to access objects from it. How are you creating and populating it? – BoltClock Feb 02 '11 at 03:50
  • I don't know anything about iPhone dev, but it looks like you're accessing index 0 of an empty array, which obviously has no index 0. – kaoD Feb 02 '11 at 03:52

4 Answers4

31

You have an empty array. You tried to call [array objectAtIndex:0]. 0 is beyond the bounds of an empty array (not surprising – anything is beyond the bounds for an empty array).

Lily Ballard
  • 182,031
  • 33
  • 381
  • 347
  • @EquinoX: Check your `NeighborMapViewController regionFromLocations` method. – BoltClock Feb 02 '11 at 03:54
  • @EquinoX: It's in `[NeighborMapViewController regionFromLocations]`. No line number is given in the stack. But if you have it in the Xcode debugger, just go to the debugger window and look in the stack trace, it should show you the line. – Ben Zotto Feb 02 '11 at 03:55
  • okay, I think I got it.. a question again.. when is the - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{ called? – aherlambang Feb 02 '11 at 04:02
  • It's called when a new location is found. You usually start the location process, and as soon as Core Location thinks it knows an approximation as to where you are, it calls that method. – sudo rm -rf Feb 02 '11 at 04:12
5

It means that early in [NeighborMapViewController regionFromLocations] you are attempting to index an NSMutableArray object. That array has zero elements in it -- it's empty. But you're apparently trying to access index 0, which would be the first element. Since there is no first element, you get an exception.

Probably the code that you expect to have executed to add items to the array has not yet executed, or you simply need to check the length of the array prior to trying to access the element at index 0. It's hard to say without knowing more about what you're doing.

gdb reports the offset as 65 bytes in to the function, so it's likely in one of the first few lines. You could probably manually inspect the code of the function to see, or set a breakpoint on the first line of the function and step through it.

  • I really have no idea where this array is – aherlambang Feb 02 '11 at 03:55
  • 1
    I assume NeighborMe is your app and NeighborMapViewController is your code - its the only thing in the stack trace that isn't obviously an Apple API. If you want help narrowing it down, post the code for [NeighborMapViewController regionFromLocations]. –  Feb 02 '11 at 04:00
0

I was seeing this error, however, not as an exception but only in the Issue Navigator. The solution was to simply restart XCode, then the error was gone.

Lukas Kalinski
  • 2,213
  • 24
  • 26
0

As Kevin Ballard said, this error is thrown when you ask for the element at index X that is beyond array bounds, in addition to looking for [array objectAtIndex: X], this error may be even caused from the modern syntax array[X]

Alessandro Muzzi
  • 808
  • 1
  • 12
  • 26