NSRangeException ', reason:' *** - [__ NSArrayM objectAtIndex:]: index 5 outside for an empty array '

in my application, when I run the application for the first time, it works ok.but, when I run 2 times two again, it crashes.

This is mistake..

NSRangeException ', reason:' *** - [__ NSArrayM objectAtIndex:]: index 5 outside for an empty array '

enter image description here

enter image description here

+9
ios objective-c uitableview
source share
4 answers

Reason:. You are accessing an empty array that is trying to access the object in the index.

replace all places as in your code below

[arrMydata objectAtIndex:indexPath.row]; 

from

  //1. Positive index ([anArray objectAtIndex:-NUMBERS]) will crash //2. within the array boundary if([arrMydata count] > 0 && [arrMydata count] > indexPath.row){ shrObj=[arrMydata objectAtIndex:indexPath.row]; } else{ //Array is empty,handle as you needed } 

** Here you can see an example without software that will explain this problem. Good luck **

+18
source share

Your array is empty, but you are trying to access the object in it. This is problem.

+3
source share

Reason: According to your log, you are trying to access empty arrays. Just fix it below code

 if (arrMydata.count > inxexPath.row) sharObj = [arrMydata objectAtIndex:indexPath.row] 
+3
source share

In case this helps someone: in my case, the array was not in the code, but at the output of IB, which was an array of 5 UIImageViews in the storyboard.

@IBOutlet var upAndDownArrowImages: [UIImageView]!

The reason for the failure was that I mistakenly deleted 1 of these UIImageViews from the storyboard.

0
source share

All Articles