Getting Argument Exception (nil URL argument)!

2.4k views Asked by At

I'm having this problem where I get this error on my iPhone:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSConcreteData initWithContentsOfURL:options:error:]: nil URL argument'

I'm trying to parse some XML data, when I try this on the simulator I get no error, but whenever I try it on a iOS device, I get the error in my console!

This is my code to parse the XML:

- (ICB_WeatherConditions *)initWithQuery:(NSString *)query {

if (self = [super init])
{
    CXMLDocument *parser = [[[CXMLDocument alloc] initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.google.com/ig/api?weather=%@", query]] options:0 error:nil] autorelease];

    condition         = [[[[[parser nodesForXPath:@"/xml_api_reply/weather/current_conditions/condition" error:nil] objectAtIndex:0] attributeForName:@"data"] stringValue] retain];        
    location          = [[[[[parser nodesForXPath:@"/xml_api_reply/weather/forecast_information/city" error:nil] objectAtIndex:0] attributeForName:@"data"] stringValue] retain];
    day2c    = [[[[[parser nodesForXPath:@"/xml_api_reply/weather/forecast_conditions/condition" error:nil] objectAtIndex:0] attributeForName:@"data"] stringValue] retain];
    day3c     = [[[[[parser nodesForXPath:@"/xml_api_reply/weather/forecast_conditions/condition" error:nil] objectAtIndex:1] attributeForName:@"data"] stringValue] retain];

    currentTemp       = [[[[[parser nodesForXPath:@"/xml_api_reply/weather/current_conditions/temp_f" error:nil] objectAtIndex:0] attributeForName:@"data"] stringValue] integerValue];
    lowTemp           = [[[[[parser nodesForXPath:@"/xml_api_reply/weather/forecast_conditions[2]/low" error:nil] objectAtIndex:0] attributeForName:@"data"] stringValue] integerValue];
    highTemp          = [[[[[parser nodesForXPath:@"/xml_api_reply/weather/forecast_conditions/high" error:nil] objectAtIndex:0] attributeForName:@"data"] stringValue] integerValue];

    conditionImageURL = [[NSURL URLWithString:[NSString stringWithFormat:@"http://www.google.com%@", [[[[parser nodesForXPath:@"/xml_api_reply/weather/current_conditions/icon" error:nil] objectAtIndex:0] attributeForName:@"data"] stringValue]]] retain];
}

return self;}

and this is my code to send the query:

- (void)showWeatherFor:(NSString *)query {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

ICB_WeatherConditions *weather = [[ICB_WeatherConditions alloc] initWithQuery:query];

self.conditionsImage = [[UIImage imageWithData:[NSData dataWithContentsOfURL:weather.conditionImageURL]] retain];

[self performSelectorOnMainThread:@selector(updateUI:) withObject:weather waitUntilDone:NO];


[pool release];}

Please help point me in the right direction! Thanks again!

2

There are 2 answers

0
sridvijay On BEST ANSWER

I FIGURED IT OUT!

Okay, so here's the story and solution: I was making a weather app, and the query needed a city in-order to get a valid XML file with data. In my simulator, the location was WAY off, so it put me in Glendale, CA?! As you can see Glendale is ONE WORD. So when this went into the query, it passed as a valid URL, and got data. When I went on my phone to test it out, I got the error stated above! This was because where I live, the city name retrieved from CLGeocoder's reverseGeocodeLocation was 2 words. (i.e Des Moines). The space in the middle messed everything up. Obviously, putting a space in a URL isn't going to get you any data back. In order to get a space in the "XML spitting service" I was using you had to use a + sign. So after about 4 hours of debugging, I found this out and I put this in:

 stringByReplacingOccurrencesOfString:@" " withString:@"+"

at the end of my method, so in the end it looked like this:

 City = [[placemark locality] stringByReplacingOccurrencesOfString:@" " withString:@"+"];

This solved it, and now spaces could be replaced with the plus signs, therefore giving me actual data back! Good Luck to anyone who has a similar problem!

1
CRD On

To get you started: the error appears to the first line within your if statement. This builds a string and a URL and the exception is "nil URL argument". So break this line up into parts:

NSString *queryPath = [NSString stringWithFormat:@"http://www.google.com/ig/api?weather=%@", query];
NSURL *queryURL = [NSURL URLWithString:queryPath];
CXMLDocument *parser = [[[CXMLDocument alloc] initWithContentsOfURL:queryURL options:0 error:nil] autorelease];

Now put in code to test everything - query, queryPath, queryURL - and produce errors if the values are not what you expect. The problem should become apparent.