0

I'm trying to parse a Facebook Page Json Feed to a table view and followed this Tutorial and I'm getting this error: "Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[__NSArrayM insertObject:atIndex:]: object cannot be nil'"

Here is my code from my tableview.h:

    #import "FBViewController.h"
    @interface FBViewController ()
    {
    NSMutableData *fbdata;
    NSMutableArray *array;
    }

    @end

    @implementation FBViewController


    - (void)viewDidLoad
    {
    [super viewDidLoad];
    [[self fbtableview]setDelegate:self];
    [[self fbtableview]setDataSource:self];
    array = [[NSMutableArray alloc]init];

    NSURL *url = [NSURL URLWithString:@"https://graph.facebook.com/XXXXXXX/posts?access_token=ACCESS_TOKEN"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    connection = [NSURLConnection connectionWithRequest:request delegate:self];

    if (connection) {
    fbdata=[[NSMutableData alloc]init];
    }
    }


    - (void)didReceiveMemoryWarning
    {
    [super didReceiveMemoryWarning];
    }


    -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
    {
    [fbdata setLength:0];
    }


    -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
    {
    [fbdata appendData:data];
    }


    -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
    {
    NSLog(@"fail with error");
    }

    -(void)connectionDidFinishLoading:(NSURLConnection *)connection
    {
    NSDictionary *allDataDictionary = [NSJSONSerialization JSONObjectWithData:fbdata options:0 error:nil];
    NSArray *arraydata = [allDataDictionary objectForKey:@"data"];//objectforkey corresponde ao nome do dicionario


    for (NSDictionary *diction in arraydata)
    {
    NSString *message =[diction objectForKey:@"message"];

    [array addObject:message];
    [[self fbtableview]reloadData];
    }
    }


    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
    return 1;
    }

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
    return [array count];
    }


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

    if (!cell)
    {
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    cell.text = [array objectAtIndex:indexPath.row];
    return cell;
    }
    @end

Can anyone help me out?

4

1 回答 1

0

问题就在这里,我想是的,

for (NSDictionary *diction in arraydata)
    {
    NSString *message =[diction objectForKey:@"message"];

    [array addObject:message]; //here some value is nil means you are trying to add a nil object to array that's why app getting crashed.
    [[self fbtableview]reloadData];
    }
    }
于 2013-05-02T04:16:22.470 回答