1

so I've used JSON serialization and been using

NSString *referencestr = [[notificationarray objectAtIndex:indexPath.row] valueForKey:@"Reference"];

and it's getting data fine and displaying it on a table view. However, when the data is a NULL, my table crashes and my app shuts down. how should I solve this problem or read the NULL as nil.

user3178926
  • 339
  • 1
  • 5
  • 15

2 Answers2

0

I'm assuming that your notificationarray was created with a call to [NSJSONSerialization JSONObjectWithData:options:encoding:]. That call will happily give you an array that contains instances of NSNull if there are null objects in the JSON being parsed.

Your client code should be robust against an uncooperative data provider. You should check the class of your [notificationarray objectAtIndex:indexPath.row]. If you're hoping for the object to be an NSDictionary then you could check like this:

id theObject = [notificationarray objectAtIndex:indexPath.row];
if ([theObject isKindOfClass:[NSDictionary class]]) {
    // Work under the assumption that theObject is a dictionary.
} else {
    // Log an error, bail out, etc.  Whatever's the appropriate
    // response for malformed data.
}
Aaron Golden
  • 7,092
  • 1
  • 25
  • 31
0

In this scenario,you can check the value whether the value is null or not.And for that make one to check it.

  if ([[notificationarray objectAtIndex:indexPath.row]valueForKey:@"Reference"]!=[NSNull null]) {
       lblDescription.text=[[notificationarray objectAtIndex:indexPath.row]valueForKey:@"Reference"];
   }
  else
   {
     //any value you wants to display in label
   }
Arpit
  • 722
  • 5
  • 15