why iOS App terminating due to NSRangeException

295 views Asked by At

I'm having a weird error popping up. It's listing that my app is

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'

What puzzles me is that the app runs perfectly fine in the simulator, but when I go to test it on my device I get that error. I've isolated the problem to the line of code below:

updatingTracker = [userDatas objectAtIndex:repeatCount];

Here is the rest of the relevant code:

[self getUserDatas];
[self timerDidFire];

-(void) getUserDatas
{
    userDatas = [[NSMutableArray alloc] init];
    NSUserDefaults *userdef = [NSUserDefaults standardUserDefaults];
    int count = [[userdef objectForKey:@"user_count"] intValue];
    for(int i=0;i<count;i++)
    {
        NewTracker *newtrack=[[NewTracker alloc] init];
        newtrack.FromCurrency = [userdef objectForKey:[NSString stringWithFormat:@"from_string_%d",i]];
        newtrack.ToCurrency   = [userdef objectForKey:[NSString stringWithFormat:@"to_string_%d",i]];
        newtrack.savedCurrency = [userdef objectForKey:[NSString stringWithFormat:@"save_currency_%d",i]];
        newtrack.userTarget = [userdef objectForKey:[NSString stringWithFormat:@"user_target_%d",i]];
        newtrack.trackerId = [userdef objectForKey:[NSString stringWithFormat:@"tracker_id_%d",i]];
        newtrack.realtimeBase = [userdef objectForKey:[NSString stringWithFormat:@"realtime_base_%d",i]];
        newtrack.realtimeTarget = [userdef objectForKey:[NSString stringWithFormat:@"realtime_target_%d",i]];
        newtrack.realtimeUSD = [userdef objectForKey:[NSString stringWithFormat:@"realtime_USD_%d",i]];
        [userDatas addObject:newtrack];
    }
}

- (void) timerDidFire
{
    NSLog(@"bug finder");

    updatingTracker = [userDatas objectAtIndex:repeatCount];
    [self chartconnectionStart:@"3m"];
}

Any ideas or help on why this is happening or how to fix it would be appreciated.

3

There are 3 answers

2
Ovi Tisler On BEST ANSWER

Your userDatas array is empty. You most likely don't have anything populated in your user defaults on your phone, so the code that adds to the array is never executed. You probably have some data already populated on your simulator and that's why it's not working on the phone

0
zaph On

Simple put a conditional breakpoint on that line for the condition [userDatas count] == 0. Or put in an if to catch that and a breakpoint on it. They use the debugger to figure out how you got there and what the associated values are.

Basic debugging.

0
Ravindra Singh On

Code like this

  • (void) timerDidFire{

    NSLog(@"bug finder");

    if([userDatas count]>0){

    updatingTracker = [userDatas objectAtIndex:repeatCount];

    [self chartconnectionStart:@"3m"];

    }

}

Happy coding..........