0

Ok, I'm a bit out of my league but here goes... I'm working on making some updates to an iPhone app for a client (it hasn't been updated since 2013...just to put into context how old the programming is). While making simple updates to the app, I noticed the twitter feed section kept crashing. I checked the debug and came up with this error.

[_NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array

I know the array is empty... I'm just not sure why. Here's the code where I think it is filling the array

- (void)loadData {
NSURL *tutorialsUrl = [NSURL URLWithString:@"https://twitter.com/EdwinOrange/lists/kentucky-general-assembly"];
NSData *tutorialsHtmlData = [NSData dataWithContentsOfURL:tutorialsUrl];

TFHpple *tutorialsParser = [TFHpple hppleWithHTMLData:tutorialsHtmlData];

NSString *tutorialsXpathQueryString = @"//p[@class='TweetTextSize js-tweet-text tweet-text']";
NSArray *tutorialsNodes = [tutorialsParser searchWithXPathQuery:tutorialsXpathQueryString];

NSMutableArray *newTutorials = [[NSMutableArray alloc] initWithCapacity:0];
for (TFHppleElement *element in tutorialsNodes) {

    NSString *strResponse = @"";

    for (int l=0; l<[[element children] count]; l++)
    {

            strResponse =[strResponse stringByAppendingString:[[[element children] objectAtIndex:l]  content]];

        NSArray *_children = [[[element children] objectAtIndex:l]  children];

        NSLog(@"count = %d",[_children count]);

        for (int k=0; k<[_children count]; k++)
        {
            NSLog(@"%@",[[_children objectAtIndex:k] children]);
            NSArray *_internalChildren = [[_children objectAtIndex:k] children];

            for (int j=0; j<[_internalChildren count]; j++)
            {
                NSLog(@"%@",[[_internalChildren objectAtIndex:j] content]);
                strResponse = [strResponse stringByAppendingFormat:@"%@",[[_internalChildren objectAtIndex:j] content]];
            }

        }

    }

    Tutorial *tutorial = [[Tutorial alloc] init];
    [newTutorials addObject:tutorial];

    tutorial.title = strResponse;

   NSLog(@"strResponse = %@",strResponse);

    NSLog(@"Data---%@",tutorial.title);

}
_data = newTutorials;

delegateObj.arrDetailContent = [_data mutableCopy];


}

It might also we worth it to note the Log does not return any information for the above code. I added an exception breakpoint which breaks at this line (because the _data array is empty)

 Tutorial *data = [_data objectAtIndex:indexPath.row];

This may also help to track down the problem (this is at the top of the TwitterController.m)

#pragma mark - Calling Twitter feeds

-(void)getAndParseTwitterFeeds
{
[self loadAtData];
[self loadNames];
[self loadData];

[self loadImages];
[self loadHours];

[self loadLink];
[self loadImages];
[self loadHours];
}

-(void)getFeeds
{
 [self getAndParseTwitterFeeds];
}

-(void)getFeedsLocally
{
_link = delegateObj.arrTwitterLink;
_objects = delegateObj.arrObjects;
_members = delegateObj.arrMemberName;
_data = delegateObj.arrDetailContent;
arrProfileImages = delegateObj.arrImages;
_hours = delegateObj.arrHour;

[self updateTableViewWithTheData];
}

Hope that is enough explanation... I'm really hoping someone can help me figure out why the array is not being filled. Thanks!!!!

EDIT: I'm starting to wonder now if the HTML parser is having trouble with images in tweets. This code is from around 2013 which pre-dates twitter's inline images. Could this be causing the array to return empty?

Also of note the arrays _link, _members, _hours, etc. are all being populated correctly and have identical coding up until the "NSString *strResponse" section for loadData.

Lauren
  • 1
  • 1
  • ... and this is caused because either `numberOfRowsInSection` is returning the wrong value, or some details of how you're populating these separate arrays is resulting in different number of items in each array. (BTW, because of the issues associated with populating multiple arrays, most of us prefer to populate a single array of custom objects which have properties for these various attributes, etc.). But to your immediate problem, add an exception breakpoint and run the code and it will pause execution at the offending line, at which point you can inspect your various arrays. – Rob Feb 26 '16 at 23:58
  • BTW, it appears that you're parsing the HTML for a twitter feed and you'd probably be better off using the Twitter API, though I suspect that's a more major conversion than you'd like to contemplate. But you want to make sure that there's not something in the HTML that's adversely affecting your HTML parsing (e.g., an ad). HTML parsing is a fragile approach, because if they change the HTML for some reason, your code might not work as expected. I'd suggest that with the aforementioned exception breakpoint, you also check the HTML and make sure it doesn't include something you weren't expecting. – Rob Feb 27 '16 at 00:02
  • 1
    In that case, you may be interested in http://stackoverflow.com/questions/3948062/nsmutablearray-initwithcapacity-nuances?lq=1, @Unheilig. – jscs Feb 27 '16 at 00:08
  • Thanks for your feedback! I really appreciate it! Unfortunately since I did not write this code I'm having to work backwards to try and figure out why it's not working. And I'm not much of a programmer, just good at logical deduction but obviously that only gets you so far when trying to fix underlying issues. I will do some research on implementing the Twitter API. Again, I appreciate the feedback! – Lauren Feb 27 '16 at 00:30
  • How is _data defined? – Phillip Mills Feb 27 '16 at 03:25
  • @PhillipMills The only other reference to _data that is not referenced in the above code snippets is this line: `@interface TwitterViewController () {NSMutableArray *_data;}` – Lauren Feb 29 '16 at 20:42

0 Answers0