Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Argument Exception (nil URL argument)!

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!

like image 901
sridvijay Avatar asked Apr 09 '26 02:04

sridvijay


1 Answers

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.

like image 149
CRD Avatar answered Apr 11 '26 18:04

CRD