2014-05-02 60 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] 
相關問題