Facebook JSON feed - __NSArrayM insertObject:atIndex:]: object cannot be nil

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?

Answers


problem is here i think so,

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];
    }
    }

Need Your Help

How to read Gradle's registry.bin?

build android-studio build-process android-gradle

I was working on Android studio (on the Mac) when I got the following message:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.