Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

JSON and NULL values errors

Anyone have a solution for dealing with NULL values that are returned when pulling JSON data?

I'm currently getting the following error message: 2014-06-11 07:11:56.829 [494:6107] LabelValue is =====> 2014-06-11 07:11:56.830[494:613] -[NSNull length]: unrecognized selector sent to instance 0x101b56b50 2014-06-11 07:11:56.846[494:613] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull length]: unrecognized selector sent to instance 0x101b56b50' *** First throw call stack: BLA BLA BLA

Example data being returned:

 {
               dateAdded = "2014-04-22T16:50:32Z";
                eventDate = "<null>"; //This is the issue #:( 
                id = 71496;
                name = Test;
                ownerName = "name";
                webLink = "someurl";
            },

2 Answers

Hi Aaron!

You might wanna check if the JSON block has a null element or not:

if (eventDate == (id)[NSNull null]) {
    // eventDate is null 
}

or you can do:

if (eventDate != (id)[NSNull null]) {
    // eventDate is not null 
    // you are free to parse it :)
}

So you won't have to add an extra else to do your parsing.

I hope this helps!

Hi Marcus, thanks I'll give this a shot.

No problem :) let me know if it works!

Yeah I can't get it to work. I'm sure I'm doing something wrong here. I'll keep working on it.

Is it still throwing the same error ?

Sure is : (

Can you show me your parser ?

      if ([self.incident objectForKey:@"eventDate"] == (id)[NSNull null]) {
                        self.labelEventDate.text = @"This is a test";

                    }

Marcus shoot me an email I'll share it with you via GitHub.