1

I need a little help, i have a search field and button which trigger this method but i get the error underneath.

- (IBAction)fetchTracks:(id)sender {

NSString *input = [searchField stringValue];
NSString *searchString = [input stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"searchString: %@", searchString);

NSError *error;
NSString* libraryPath = [[NSString alloc] initWithString:@"~/Music/iTunes/iTunes Music Library.xml"];
NSURL *url = [NSURL URLWithString:libraryPath];

[doc release];
doc = [[NSXMLDocument alloc] initWithContentsOfURL:url options:0 error:&error];

NSLog(@"doc: %@", doc);

[itemNodes release];
itemNodes = [[doc nodesForXPath:@"ItemSearchResponse/Items/Item" error:&error] retain];

[tableView reloadData];
}

* -[NSXMLDocument initWithContentsOfURL:options:error:]: nil argument

Can anyone help, thanks, Sami.

Sami
  • 1,374
  • 1
  • 16
  • 43
  • can you post the output of the console too. – Robin Feb 14 '11 at 16:52
  • I changed &error to NULL and used libraryPath = [libraryPath stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]; this seems to have solved the error, but now in the console i get "doc: (null)". – Sami Feb 14 '11 at 18:13

3 Answers3

1

Change this line:

NSError *error;

To this:

NSError *error = nil;

Read this question for additional detail as to why this works: NSError: Does using nil to detect Error actually turn off error reporting?


However, you don't seem to care about the error object at all so you would be better off just calling these methods like this (replace &error with NULL)

doc = [[NSXMLDocument alloc] initWithContentsOfURL:url options:0 error:NULL];
...
itemNodes = [[doc nodesForXPath:@"ItemSearchResponse/Items/Item" error:NULL] retain];
Community
  • 1
  • 1
theChrisKent
  • 15,029
  • 3
  • 61
  • 62
1

You need to escape spaces in your code. E.g., like this

  libraryPath = [libraryPath stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];

When I add this, your code doesn't crash.
But generally, I would recommend accessing files as files (e.g. NSString stringWithContentsOfFile). It will make your life easier.

You can see the list of characters URL can't contain here:

Unsafe:

Characters can be unsafe for a number of reasons. The space character is unsafe because significant spaces may disappear and insignificant spaces may be introduced when URLs are transcribed or typeset or subjected to the treatment of word-processing programs.

Community
  • 1
  • 1
Nikita Rybak
  • 67,365
  • 22
  • 157
  • 181
0

I think you might need:

NSString *urlString = [@"~/Music/iTunes/iTunes Music Library.xml" stringByExpandingTildeInPath];
NSString* libraryPath = [[NSString alloc] initWithString:urlString];
Abizern
  • 146,289
  • 39
  • 203
  • 257