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!

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 Build a Blog Reader iPhone App Getting Data from the Web Downloading and Parsing JSON Data

Mike Lange
Mike Lange
794 Points

"Uncaught exception" for no apparent reason

I've checked my code against the tutorial, all looks good. and the program runs successfully (and verified JSON data is received) up to this point:

NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error]; // JSON serialisation converts binary to readable format

Error data below:

014-12-02 22:25:52.168 Blog from scratch[19718:958710] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'
*** First throw call stack:
    0   CoreFoundation                      0x000000010c2873f5 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010bf20bb7 objc_exception_throw + 45
    2   CoreFoundation                      0x000000010c28732d +[NSException raise:format:] + 205
    3   Foundation                          0x000000010bbc5c4f +[NSJSONSerialization JSONObjectWithData:options:error:] + 67
    4   Blog from scratch                   0x000000010b9f54d5 -[TableViewController viewDidLoad] + 197
    5   UIKit                               0x000000010c798190 -[UIViewController loadViewIfRequired] + 738
    6   UIKit                               0x000000010c79838e -[UIViewController view] + 27
    7   UIKit                               0x000000010c6b7db9 -[UIWindow addRootViewControllerViewIfPossible] + 58
    8   UIKit                               0x000000010c6b8152 -[UIWindow _setHidden:forced:] + 276
    9   UIKit                               0x000000010c6c465c -[UIWindow makeKeyAndVisible] + 42
    10  UIKit                               0x000000010c66f191 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 2628
    11  UIKit                               0x000000010c671e5c -[UIApplication _runWithMainScene:transitionContext:completion:] + 1350
    12  UIKit                               0x000000010c670d22 -[UIApplication workspaceDidEndTransaction:] + 179
    13  FrontBoardServices                  0x000000010ee292a3 __31-[FBSSerialQueue performAsync:]_block_invoke + 16
    14  CoreFoundation                      0x000000010c1bcabc __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
    15  CoreFoundation                      0x000000010c1b2805 __CFRunLoopDoBlocks + 341
    16  CoreFoundation                      0x000000010c1b25c5 __CFRunLoopRun + 2389
    17  CoreFoundation                      0x000000010c1b1a06 CFRunLoopRunSpecific + 470
    18  UIKit                               0x000000010c670799 -[UIApplication _run] + 413
    19  UIKit                               0x000000010c673550 UIApplicationMain + 1282
    20  Blog from scratch                   0x000000010b9f5a33 main + 115
    21  libdyld.dylib                       0x000000010e7ff145 start + 1
libc++abi.dylib: terminating with uncaught exception of type NSException

So I blocked the NSJSONSerialization from running and somehow jsonData is reported as empty after these 3 lines of code:

NSURL *blogURL = [NSURL URLWithString:@"http://blog.teamtreehouse.com/api/get_recent_summary/"];

    NSData *jsonData = [NSData dataWithContentsOfURL:blogURL];

    NSError *error = nil;


self = TableViewController * 0x7fe89875bcf0

error = NSError * nil

blogURL = NSURL * @"http://blog.teamtreehouse.com/api/get_recent_summary/"

jsonData = NSData * nil 0x0000000000000000

dataDictionary = NSDictionary * 0x3ff0000000000000 0x3ff0000000000000


So when running these lines:

NSURL *blogURL = [NSURL URLWithString:@"http://blog.teamtreehouse.com/api/get_recent_summary/"];

NSData *jsonData = [NSData dataWithContentsOfURL:blogURL];

It looks like "dataWithContentsOfURL" isn't handling the data properly?

How can I confirm what's causing it to fail?

/// FIXED - restarted xcode.

1 Answer

Mike Lange
Mike Lange
794 Points

Ok after wasting a LOT OF TIME on this I restarted XCODE and the problem is gone.

I really didn't expect XCODE to be unstable and cause a problem like this but now I know, and maybe this saves someone else time in future if they get a strange uncaught exception for no apparent reason.

Noe Alejandro
Noe Alejandro
3,615 Points

If it wasn'tยดt for your comment I would've spent days trying to figure this out because I normally leave Xcode running.

Nadav Etinzon
Nadav Etinzon
4,375 Points

You sir are a scholar and a gentleman. Saved me quite a while of head scratching, thanks!