0

I was testing an app on Xcode and I got this error saying that an exception has been thrown. Using the debugger, I added an exception breakpoint which led me to this:

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    self.navigationController.delegate = self;

    int index = [[NSUserDefaults standardUserDefaults] integerForKey:@"ChecklistIndex"];
    if (index != -1) {
        Checklist *checklist = [self.dataModel.lists objectAtIndex:index];
        [self performSegueWithIdentifier:@"ShowChecklist" sender:checklist];
    }
}

With this being the highlighted line:

Checklist *checklist = [self.dataModel.lists objectAtIndex:index];

And the debugger said this:

-[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array

I'm not too sure how to fix this. If any more information is needed let me know. Thanks.

4
  • Uh, what does the error message say???? Did you actually read it?? Commented Mar 15, 2014 at 22:47
  • 1
    As @HotLicks said, it's pretty self-explanatory. The error is telling you are trying to access index 0 of an empty array, if you attempt an access an index in an array that doesn't exist, you will throw an exception. Commented Mar 15, 2014 at 22:49
  • probably with @try {} @catch {} block...? Commented Mar 16, 2014 at 1:27
  • 1
    @holex - Try/catch is rarely the answer in Objective-C, especially for this problem. Commented Mar 16, 2014 at 2:39

1 Answer 1

1

It looks like your self.dataModel.lists property is empty.

You should look at where you actually set that ".lists" property or "dataModel" property. One of them is not what you think it is.

Sign up to request clarification or add additional context in comments.

1 Comment

Yep, that sounds right. Turns out because the dataModel property contained no checklists objects in the "lists" array. And this caused the exception. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.