Crashes with [__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array error

30,289

Solution 1

One way to debug this issue is to add a Symbolic Breakpoint on objectAtIndex: This may result in many hits to objectAtIndex: before the debugger hits the one you're after but it's guaranteed to find it.

Steps in Xcode 4:

  • View > Navigators > Breakpoint Navigator
  • click '+' and choose Add Symbolic Breakpoint in the pop-up
  • type in "objectAtIndex:" in the Symbol text field and click Done

at runtime, if you can't tell which objectAtIndex: you're hitting, move the slider at the bottom of the Debug Navigator all the way to the right.

Solution 2

This will stop the debugger at the offending line:

Xcode/Edit Scheme/Diagnostics/Log Exceptions.

Solution 3

This error means, that you try to get an object from a NSArray from a position where no object is found. In other words: if the index is 0, than your array is empty.
And most likely it is your code, as main() invokes your code.

Solution 4

You need to search your project for all usages of objectAtIndex: and rule out each one as the culprit. Make sure you are never calling it on an empty array.

Another tip: If you know you only want to get the last object in an array, or you know the array will only ever contain one object, you can use lastObject instead of objectAtIndex: -- it's safer because it won't throw an exception, though you still need to check for nil.

Share:
30,289
Admin
Author by

Admin

Updated on June 13, 2020

Comments

  • Admin
    Admin almost 4 years

    I am getting crash reports via users of my iOS app, but crash I can't reproduce myself, nor can I trace the error back to a line in my own code (except that it originates from line 14 in main.m but this is the default application creation of iOS apps. Below the a crash report I received.

    In understand that at some point a object is at index 0 is being retrieved from an empty Array. But since it doesn't seem to point to my own code, can it be a bug in iOS? (It happens on different platforms and with different iOS versions).

    I hope someone has an idea what is happening or can point me in the right direction. Thanks.

    CRASH REPORT:

    Incident Identifier: [TODO]
    CrashReporter Key:   [TODO]
    Process:         Mary Black [797]
    Path:            /var/mobile/Applications/28A68F8B-294E-4B86-9E75-ED5484E5EF4D/Mary Black.app/Mary Black
    Identifier:      net.broset.Mary-Black
    Version:         225
    Code Type:       ARM (Native)
    Parent Process:  launchd [1]
    
    Date/Time:       2011-10-14 03:47:32 +0000
    OS Version:      iPhone OS 5.0 (9A334)
    Report Version:  104
    
    Exception Type:  SIGTRAP
    Exception Codes: #0 at 0x35b07848
    Crashed Thread:  0
    
    Application Specific Information:
    *** Terminating app due to uncaught exception \\\'NSRangeException\\\', reason: \\\'*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array\\\'
    
    Thread 0 Crashed:
    0   libsystem_kernel.dylib              0x00010848 __kill + 8
    1   CoreFoundation                      0x000b9987 __handleUncaughtException + 75
    2   libobjc.A.dylib                     0x000092d1 _objc_terminate + 129
    3   libc++abi.dylib                     0x000043c5 _ZL19safe_handler_callerPFvvE + 77
    4   libc++abi.dylib                     0x00004451 operator delete(void*) + 1
    5   libc++abi.dylib                     0x00005825 __cxa_current_exception_type + 1
    6   libobjc.A.dylib                     0x00009235 objc_exception_rethrow + 13
    7   CoreFoundation                      0x0000f545 CFRunLoopRunSpecific + 405
    8   CoreFoundation                      0x0000f3a5 CFRunLoopRunInMode + 105
    9   GraphicsServices                    0x00003fed GSEventRunModal + 157
    10  UIKit                               0x00031743 UIApplicationMain + 1091
    11  Mary Black                          0x00002fa7 main (main.m:14)
    
  • Admin
    Admin over 12 years
    I will try, but the problem is that I never experienced it on any of my own devices or in the simulator. As I have never been able to reproduce it, how will I encounter it with debugging?
  • Admin
    Admin over 12 years
    Very handy tip. Don't know yet if it solves my issue, but there are several places where it can make my code simpler (as the result is allowed to be nil).
  • maz
    maz over 12 years
    It can be difficult to fix a bug if you can't reproduce the issue yourself. As a matter of fact, you'll notice that in most bug-tracking systems there is usually a "Steps To Reproduce" section. Bug reproducibility is probably the largest step to actually fixing a bug. If you can't reproduce, you'll need to collect a lot of logs and do log analysis.
  • Byte
    Byte almost 12 years
    If only this works :), It does not work with my current bug :(
  • Thorsten Niehues
    Thorsten Niehues over 11 years
    No It doesn't. What version of Xcode do u have? What other settings do we need to set?