0

Please consider this code:

 - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view from its nib.

        NSURL *url = [NSURL URLWithString:@"http://localhost/faq.php?faqType=2"]; // Modify this to 
        NSString *jsonreturn = [[NSString alloc] initWithContentsOfURL:url]; // Pulls the URL
        NSLog(@"jsonreturn=%@",jsonreturn); // Look at the console and you can see what the restults are
        NSData *jsonData = [jsonreturn dataUsingEncoding:NSUTF32BigEndianStringEncoding];
        NSError *error = nil;

        // In "real" code you should surround this with try and catch
        NSDictionary *dict = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:&error];

        if (dict)
        {
            rows = [[dict objectForKey:@"faq"] retain];
        }
        [jsonreturn release];    
    }


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
  {

        // Configure the cell.
        static NSString *CellIdentifier = @"Cell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
        }

        NSDictionary *dict1 = [rows objectAtIndex:indexPath.row];
        NSLog(@"%@", dict1);
        cell.textLabel.text = [dict1 objectForKey:@"faqQues"];
  }


//if it is not getting NULL value then UItableView is ok
{"faq":[{"faqQues":"this is mr.mack?"},{"faqQues":"is he good man?"}]}

//but if the data is like NULL 

    {"faq":[{"faqQues":"this is mr.mack?"},{"faqQues":null}]}  // then it is creating EXEC_BAD_ACCESS error, 

so how to avoid NULL or check null value, or how can i fix this issue?

3 Answers 3

2

You can query the value before using it.

if ([dict objectForKey:@"faqQues"] == [NSNull null]) {
    // value is null, use your own value here
} else {
    // good value to use
}

You can also do this while enumerating as well.

for (id value in dict) {
    if (value == [NSNull null]) {
        // null
    }

}
Sign up to request clarification or add additional context in comments.

Comments

0

The docs of TouchJSON state, that JSON null values are represented using the NSNull singleton (usually used to represent nil in collections - where nil is not allowed). So you have to check against [NSNull null].

But TouchJSON allows to override the default null object:

CJSONDeserializer *theDeserializer = [CJSONDeserializer deserializer];
theDeserializer.nullObject = NULL;

Details https://github.com/TouchCode/TouchJSON (see the "Avoiding NSNull values in output." section)

Comments

0

My fellow programmer Conrad Kramer actually came up with this: https://gist.github.com/3362607

I would change it to a function and replace NULL values with @""

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.