Tableview crashing when JSON returns null ios
<p>I am using the google places api to search for nearby restaurants. Each restaurant is displayed in a cell in a UITableView. The return response is in the JSON format. Of the the things the JSON contains is a decimal number that represents the rating of the restaurant. I want to display the rating in the cells. However sometimes there is no rating for the restaurant and the value is (null). So I added a check in the cellForRowAtIndexPath method that checks if the value of the rating is null or not.</p>

<pre><code>if([dict valueForKey:kResponseRating] != [NSNull null])
{
    NSNumber *rating = [dict valueForKey:kResponseRating];
    [cell displayRating:rating];
}
else
{
    NSLog(@"Value of rating is null");
}
</code></pre>

<p>When I run the application the tableView still crashes as soon as a null value is returned and the string "Value of rating is null" is NOT printed. So its not going into the else statement even tho the value in the json is null.</p>

<p>Ok so I checked if the return value is of type NSString class and its not. Here is what i did in the same method:</p>

<pre><code>if([[dict objectForKey:kResponseRating] isKindOfClass:[NSString class]])
{
    NSLog(@"The return response is of type NSString");
}
</code></pre>

<p>And it did not go into if statement.</p>

<p>Here is the method that calculates the rating and posts it. The method takes the rating rounds it to the nearest .5 and then displays it as stars out of 5. </p>

<pre><code>-(void)displayRating:(NSNumber*)rating{

double rate = [rating doubleValue];


rate = round(rate * 2.0) / 2.0;


int counter = 0;
double compareVar = 1.0;

while(counter &lt;= 4){

  if(rate == 0.0)
  {
      imgRating[counter] = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"starempty.png"]];
      [self.contentView addSubview:imgRating[counter]];
  }
  else
  {

    if(compareVar &lt; rate)
    {
        imgRating[counter] = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"starfill.png"]];
        [self.contentView addSubview:imgRating[counter]];
    }
    else if(compareVar == rate)
    {
        imgRating[counter] = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"starfill.png"]];
        [self.contentView addSubview:imgRating[counter]];
    }
    else
    {
        if(compareVar - rate == 0.5)
        {
            imgRating[counter] = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"starhalffill.png"]];
            [self.contentView addSubview:imgRating[counter]];
        }
        else
        {
            imgRating[counter] = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"starempty.png"]];
            [self.contentView addSubview:imgRating[counter]];
        }

   }

    counter++;
    compareVar = compareVar + 1.0;

  }
}

   NSLog(@"This is rate: %f", rate);
}
</code></pre>

<p>Is a line of code in this method causing a crash?</p>