Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

UITableViewController [__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array' with PFTask thenCallBackOnMain

I am purposely creating a empty Array to not display anything on the UITablewView.

However, it gives me that error.

To debug, I even created an empty UITableViewController and refer storyboard file to this. However, it is giving me the same error.

I just tried and connect it with an empty UIViewController, it is giving me the same objectAtIndex error.

So I doubt it is the problem with the what I am indexing for cells.

When I run, the screen is shown but it throws the error and it freezes.

This is what I have for the UITableViewController.

- (void)viewWillAppear:(BOOL)animated
{

    [super viewWillAppear:animated];

    self.currentUser = appDelegate.currentUser;

    NSString *addNewsFeed = @"_NewsFeed";


    if (self.currentUser)
    {


        if (appDelegate.selectedGroup == nil)
        {
            self.newsList = nil;


        }
        else
        {
            NSLog(@"SELECTED GROUP EXIST");
            NSString *currentNewsFeed = [appDelegate.selectedGroup[@"name"] stringByAppendingString:addNewsFeed];

            PFQuery *query = [PFQuery queryWithClassName:currentNewsFeed];
            [query orderByDescending:@"createdAt"];

            [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {

                if (error)
                {
                    NSLog(@"Error: %@, %@", error, [error userInfo]);
                }
                else
                {
                    self.newsList = objects;
                    [self.tableView reloadData];
                }
            }];
        }



    }
    else
    {
        NSLog(@"%@", appDelegate.currentUser);
        [self performSegueWithIdentifier:@"loginView" sender:self];

    }

    NSLog(@"ZXCVZCVZ: %@", self.newsList);



}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (appDelegate.selectedGroup == nil)
    {
        NSLog(@"NO CELL HERE");

        return 0;
    }

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (appDelegate.selectedGroup == nil)
    {
        NSLog(@"NO CELL");
        return 0;
    }

    return [self.newsList count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    NSLog(@"NO LIST FOUND");
    static NSString *CellIdentifier = @"News";
    NSLog(@"DSFSDFSDFSFS");
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    PFObject *item = [self.newsList objectAtIndex:indexPath.row];
    cell.textLabel.text = item[@"title"];
    cell.detailTextLabel.text = item[@"news"];

    return cell;



}

2 Answers

would you mind sharing the project through dropbox? That way it's much easier for me (or someone else) to fix/find the error :)

I would like to help, but could you refer to which of the courses here in which you got the error so I can check against my own code for that course please.