0

I have searched through a number of these NSRangeException error threads but can't seem to find an answer to my question. Here is my error:

2015-08-15 17:28:43.793 UTK Recruiting[8794:254203] * Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayI objectAtIndex:]: index 3 beyond bounds [0 .. 2]' *** First throw call stack:

Here is a couple of bits of code. Basically I am going through the documents directories, getting all the filenames and paths, and then I'm trying to populate UITableView with the filenames so a cell can be selected, and then that file can be attached to an email. Here are a few bits of my code:

Parses documents directory, puts all csv files in corresponding arrays.

- (void) refreshTable {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSArray *documentArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:nil];

    NSArray *csvFiles = [documentArray filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(NSString *evaluatedObject, NSDictionary *bindings) {
        return [evaluatedObject hasSuffix:@".csv"];
    }]];

    self.csvFileNames = (NSMutableArray*) csvFiles;
    self.csvFilePaths = [NSMutableArray arrayWithCapacity:[csvFiles count]];

    for (NSString *fileName in csvFiles) {
        [self.csvFilePaths addObject:[documentsDirectory stringByAppendingPathComponent:fileName]];
    }



    NSLog(@"files array %@", _csvFileNames);
    NSLog(@"files array %@", _csvFilePaths);

This is the UITableView code:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    NSLog(@"count: %lu", (unsigned long)[self.csvFileNames count]);
    return [self.csvFileNames count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellforRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    cell.textLabel.text=[self.csvFileNames objectAtIndex:indexPath.row];

    return cell;
}

After placing break points throughout my code, I've found that it crashes in the cellForRowAtIndexPath method, which is the last method in the code I posted. The cellForRowAtIndexPath method does not run at all. Something else important to note is that there are 4 csv files, and [self.csvFileNames count] correctly gives the number 4.

The code should be running until index 3, but I can't figure out why it is not.

bt all crash report:

  • thread #1: tid = 0x56469, 0x0000000109200286 >libsystem_kernel.dylib__pthread_kill + 10, queue = 'com.apple.main-thread', stop >reason = signal SIGABRT frame #0: 0x0000000109200286 libsystem_kernel.dylib__pthread_kill + 10 frame #1: 0x000000010923342f libsystem_pthread.dylibpthread_kill + 90 frame #2: 0x0000000108fa019a libsystem_sim_c.dylibabort + 129 frame #3: 0x0000000108d8b481 libc++abi.dylibabort_message + 257 frame #4: 0x0000000108db33d5 libc++abi.dylibdefault_terminate_handler() + 267 frame #5: 0x0000000101e4be19 libobjc.A.dylib_objc_terminate() + 103 frame #6: 0x0000000108db0b01 libc++abi.dylibstd::__terminate(void (*)()) + 8 frame #7: 0x0000000108db07aa libc++abi.dylib__cxa_rethrow + 99 frame #8: 0x0000000101e4bd2c libobjc.A.dylibobjc_exception_rethrow + 40 frame #9: 0x00000001020db41e CoreFoundationCFRunLoopRunSpecific + 654 frame #10: 0x0000000104e74a3e GraphicsServicesGSEventRunModal + 161 frame #11: 0x00000001025ab8c0 UIKit`UIApplicationMain + 1282

    • frame #12: 0x000000010176311f UTK Recruitingmain(argc=1, argv=0x00007fff5e4a04f8) + 111 at main.m:14 frame #13: 0x0000000108ef8145 libdyld.dylibstart + 1 frame #14: 0x0000000108ef8145 libdyld.dylib`start + 1

    thread #2: tid = 0x564a0, 0x0000000109201232 libsystem_kernel.dylibkevent64 + 10, queue = 'com.apple.libdispatch-manager' frame #0: 0x0000000109201232 libsystem_kernel.dylibkevent64 + 10 frame #1: 0x0000000108eb376c libdispatch.dylib_dispatch_mgr_invoke + 247 frame #2: 0x0000000108eb3511 libdispatch.dylib_dispatch_mgr_thread + 54

    thread #3: tid = 0x564a2, 0x000000010920094a libsystem_kernel.dylib__workq_kernreturn + 10 frame #0: 0x000000010920094a libsystem_kernel.dylib__workq_kernreturn + 10 frame #1: 0x00000001092316c3 libsystem_pthread.dylib_pthread_wqthread + 869 frame #2: 0x000000010922f40d libsystem_pthread.dylibstart_wqthread + 13

    thread #4: tid = 0x564a3, 0x000000010920094a libsystem_kernel.dylib__workq_kernreturn + 10 frame #0: 0x000000010920094a libsystem_kernel.dylib__workq_kernreturn + 10 frame #1: 0x00000001092316c3 libsystem_pthread.dylib_pthread_wqthread + 869 frame #2: 0x000000010922f40d libsystem_pthread.dylibstart_wqthread + 13

    thread #5: tid = 0x564a4, 0x000000010920094a libsystem_kernel.dylib__workq_kernreturn + 10 frame #0: 0x000000010920094a libsystem_kernel.dylib__workq_kernreturn + 10 frame #1: 0x00000001092316c3 libsystem_pthread.dylib_pthread_wqthread + 869 frame #2: 0x000000010922f40d libsystem_pthread.dylibstart_wqthread + 13

    thread #6: tid = 0x564a5, 0x000000010920094a libsystem_kernel.dylib__workq_kernreturn + 10 frame #0: 0x000000010920094a libsystem_kernel.dylib__workq_kernreturn + 10 frame #1: 0x00000001092316c3 libsystem_pthread.dylib_pthread_wqthread + 869 frame #2: 0x000000010922f40d libsystem_pthread.dylibstart_wqthread + 13

Crashing in main.m on the return statement:

int main(int argc, char * argv[]) {
@autoreleasepool {
    return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}

UPDATE: I have been browsing many other NSRangeException threads, so far with no luck.

  • 1
    To see the actual statement that is causing the error add an exception breakpoint: 1. From the Main Menu Debug:Breakpoints:Create Exception Breakpoint. 2. Right-click the breakpoint and set the exception to Objective-C. 3. Add an action: "po $arg1". Run the app to get the breakpoint and you will be at the line that causes the exception and the error message will be in the debugger console. – zaph Aug 19 '15 at 14:14
  • Are you calling `[self.tableView reloadData]` after you edit `csvFileNames`? – Brian Nickel Aug 19 '15 at 14:15
  • And are you only changing `csvFileNames` in the main thread? – Brian Nickel Aug 19 '15 at 14:17
  • @zaph that is not giving me a line number. This is what is printed in the debugger console: *** -[__NSArrayI objectAtIndex:]: index 3 beyond bounds [0 .. 2] – Alex Cauthen Aug 19 '15 at 14:21
  • @Brian I am not calling that after I edit data in csvFileNames. Where exactly would I place that statement, refreshTable or in one of the TableView methods? The only place it is edited is in the refreshTable method. The refreshTable method is running on the main thread. – Alex Cauthen Aug 19 '15 at 14:26
  • Does adding reloadData after setting `csvFileNames` change anything? Is there anywhere where you do a `[self.csvFileNames removeObject:]`? – Brian Nickel Aug 19 '15 at 14:29
  • I never remove anything from csvFileNames. It can only technically be added to. I added the reloadData statement after csvFileNames is loaded with the contents of csvFiles in the refreshTable method. That might not be the correct place to add it. It caused the program to crash at the line in which the reloadData statement was added. – Alex Cauthen Aug 19 '15 at 14:35

2 Answers2

0

If you are thinking its from UITableView it will not call from threads, It always call from main thread only. Try adding Exceptional Breakpoint in the project as shown in the screenshot, so it will stop right before crash happen. You can find the crashing line and identify where exactly the issue happening

Add Exceptional Breakpoint

Sumanth
  • 4,913
  • 1
  • 24
  • 39
  • It says it is crashing in main. m on the following line: return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); I'll update the original post of the new code – Alex Cauthen Aug 19 '15 at 14:03
0

Of course it crashes ot the mainthread - its an uncaught exception. Adding a breakpoint for all exceptions should highlight [self.csvFileNames objectAtIndex:indexPath.row].

The property system of iOS never fails.

Only explanation I can think of is your array get nulled/modified while the table view gets populated.

Check how often and from where the method refreshTable gets called. It should be once.

Also cant harm to put an NSLog in the tableView:cellforRowAtIndexPath: and validate that the array is unchanged during execution.

another, but unusual and probably unlikely, explanation could be a modified NSArray Class. (for instance a Category for NSArray that changes the behaviour of objectAtIndex:. Was common once. But you should know if you did add any NSArray/NSMutableArray Categories.

A replacement for the method call could be keyed subscript.

cell.textLabel.text = self.csvFileNames[indexPath.row];

Helge Becker
  • 3,219
  • 1
  • 20
  • 33