Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'NSInvalidArgumentException', reason: 'data parameter is nil'

XCode newbie here and i have been trying to figure out why the following is happening...

With the way the code is currently written, i keep getting 'NSInvalidArgumentException'.

reason: 'data parameter is nil' in xcode. The url works fine on the browser.

When I remove the "filters=%7B%22region%22%3A%22CA%22%7D" part of the url, it works fine in Xcode, but when this section of the url is included, that's when I get the error message. I have tried using \" in replacement of the %22 but still nothing. Any suggestions are greatly appreciated.

NSURL *url = [NSURL URLWithString:[@"http://api.v3.factual.com/t/restaurants-us?q=peets+coffee&filters=%7B%22region%22%3A%22CA%22%7D&KEY=p7kwKMFUSyVi64FxnqWmeSDEI41kzE3vNWmwY9Zi"stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData *data = [NSData dataWithContentsOfURL: url];
like image 881
user1254909 Avatar asked Mar 07 '12 14:03

user1254909


4 Answers

I also had the same problem:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'

But I had solved it. The reason is my API URL is incorrect. Please check out your API URL. Good luck for you!

like image 185
luofubin Avatar answered Nov 02 '22 17:11

luofubin


The code you have posted works without crashing, but the data object ends up being nil

At some point later in the code you access data. Since data == nil, the app crashes.

I suggest entering

http://api.v3.factual.com/t/restaurants-us?q=peets+coffee&filters=%7B%22region%22%3A%22CA%22%7D&KEY=p7kwKMFUSyVi64FxnqWmeSDEI41kzE3vNWmwY9Zi

and

http://api.v3.factual.com/t/restaurants-us?q=peets+coffee&KEY=p7kwKMFUSyVi64FxnqWmeSDEI41kzE3vNWmwY9Zi

into a browser to shed some light on the situation

UPDATE

The problem was encoding already encoded strings. (The %'s are being encoded to %25)

NSString *urlBase = @"http://api.v3.factual.com/t/restaurants-us?";
NSString *urlData = [@"q=peets+coffee&filters={\"region\":\"CA\"}&KEY=p7kwKMFUSyVi64FxnqWmeSDEI41kzE3vNWmwY9Zi"stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",urlBase,urlData]];
NSData* data = [NSData dataWithContentsOfURL: url];

Works, because you are encoding a non-encoded string.

OR

NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"http://api.v3.factual.com/t/restaurants-us?q=peets+coffee&filters=%7B%22region%22%3A%22CA%22%7D&KEY=p7kwKMFUSyVi64FxnqWmeSDEI41kzE3vNWmwY9Zi"]];
NSData* data = [NSData dataWithContentsOfURL: url];

Will work because as your post is, your string was already encoded properly

like image 32
Jesse Black Avatar answered Nov 02 '22 19:11

Jesse Black


I had the same issue and I found one word in service name in upper case while actually it was in lower on server. I did fixed the name in URL and issue got resolved. Thanks.

like image 1
Sid Avatar answered Nov 02 '22 17:11

Sid


I think the app crash at offline mode and you tried to NSJSONSerialization the data. Thanks

like image 1
Fadia Jaradat Avatar answered Nov 02 '22 19:11

Fadia Jaradat