2014-05-02 63 views
7

当我第一次运行应用程序时,它工作正常。但是当我再次运行两次两次,它崩溃。NSRangeException',原因:'*** - [__ NSArrayM objectAtIndex:]:索引5超出空数组的范围'

这是错误..

NSRangeException”,原因: '*** - [__ NSArrayM objectAtIndex:]:索引5超出界限为空数组'

enter image description here

enter image description here

+0

'arrMydata'数组似乎是空的。那就是崩溃日志所告诉的。 – GoodSp33d

+1

arrMydata应该分配] init'ed或者它应该有arrayWithCapacity:// somevalue之前添加对象到arrMydata –

+0

@ 2-Stroker我知道,但我找不到解决方案。 –

回答

16

原因:您正在访问空数组即将访问索引处的对象。

//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 the non software example, which will explain this issue. Good luck! **

+0

我认为,只是这['arrMydata count]> inxexPath.row'检查就足够了。 – Mani

+0

有1.正面指数 –

+0

你们都对了 –

3

你的数组是空的,但你试图访问它中的一个对象。那就是问题所在。

3

原因更换像所有的地方在你的代码下面

[arrMydata objectAtIndex:indexPath.row]; 

根据你的日志,你要访问空数组。只需通过以下代码修复此问题

if (arrMydata.count > inxexPath.row) 
    sharObj = [arrMydata objectAtIndex:indexPath.row] 
相关问题