0

I inherited the following code:

- (id)initWithFileURL:(NSURL *)aURL {
    if((self = [super initWithFileURL:aURL]) != nil) {

This init method is in a class which inherits from UIManagedDocument

When it hits the "if" line, I'm getting:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSURL initFileURLWithPath:]: nil string parameter'

So far, I've been unable to figure out why. I've tested this code in the legacy project I took it from, it works fine there. The method is getting called with aURL populated (aURL is not nil, seems ok) in both projects. What could be wrong with the new project I've copied this legacy code into, to be causing it to throw this error? Thanks.

Edit added:

Inserting NSLog immediately before the "if" line outputs the following value for aURL

file:///var/mobile/Applications/427BABD3-9D9D-41B7-8B99-1586E93FFADD/Documents/3FE3861A-CA4B-4859-B14B-5CC4A1C6E7B4.khp

Edit added, here is the code higher up the stack trace:

- (id)initWithFileURL:(NSURL *)aURL withFormType:(KHFormType)aFormType {
    if((self = [self initWithFileURL:aURL]) != nil) {
        self.formType = aFormType;
    }
    return self;
}
...


NSURL *uniqueFileURL = [[self class] uniqueFilenameInDirectory:documentURL withFileExtension:DOCUMENT_EXT];

//    KHDocument *aDocument = [[KHDocument alloc] initWithFileURL:uniqueFileURL withFormType:inputPaceForm];
    NSLog(@"%d", [KHProfileInfoController getFormType:@"inputPaceForm"]);
    KHDocument *aDocument = [[KHDocument alloc] initWithFileURL:uniqueFileURL withFormType:[KHProfileInfoController getFormType:@"inputPaceForm"]];
...

+ (NSURL *)uniqueFilenameInDirectory:(NSURL *)dirURL withFileExtension:(NSString *)fileExtension {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *existingFiles = [fileManager contentsOfDirectoryAtPath:[dirURL path] error:nil];
    NSString *uniqueFilename;

    do {
        CFUUIDRef newUniqueId = CFUUIDCreate(kCFAllocatorDefault);
        CFStringRef newUniqueIdString = CFUUIDCreateString(kCFAllocatorDefault, newUniqueId);

        uniqueFilename = [[[dirURL path] stringByAppendingPathComponent:(__bridge NSString *)newUniqueIdString] stringByAppendingPathExtension:fileExtension];

        CFRelease(newUniqueId);
        CFRelease(newUniqueIdString);
    } while ([existingFiles containsObject:uniqueFilename]);

    return [NSURL fileURLWithPath:uniqueFilename];
}
Bradley Thomas
  • 4,060
  • 6
  • 33
  • 55
  • 3
    because aURL is nil here, before calling super init , you should check, if aURL is nill or not. if(aURL) will do it. – Pawan Rai Aug 18 '14 at 14:21
  • The `aURL` argument is `nil`. – Guillaume Algis Aug 18 '14 at 14:21
  • aURL does not appear nil to me. I just did NSLog before the if line, NSLog printed out the value. Stepping through debug, still got the same crash on the next line. – Bradley Thomas Aug 18 '14 at 14:26
  • You error says that there's something wrong with `initFileURLWithPath`, but the code you posted only shows `initWithFileURL`. Can you post more code please? – jbouaziz Aug 18 '14 at 14:46
  • @jbouaziz, yes that is puzzling me also. What other code would you like to see? I'm not sure what else would be relevant? – Bradley Thomas Aug 18 '14 at 14:52
  • The call to `initWithFileURL:` if you can. – jbouaziz Aug 18 '14 at 14:57
  • OK @jbouaziz, did that and also posted some extra info I discovered when I tried checkResourceIsReachableAndReturnError – Bradley Thomas Aug 18 '14 at 15:13
  • Found this link https://github.com/MarcoSero/Nimble/issues/16 ... so I'm now wondering whether this is related to me not having my Managed Object model set up properly. – Bradley Thomas Aug 18 '14 at 15:18
  • @BradThomas Apparently error 260 means that the file can not be found. This could help http://stackoverflow.com/a/10679741/1835155 – jbouaziz Aug 18 '14 at 15:22
  • I removed the reference to error 260 in my question since I was getting the same thing in the legacy project which is working. So that's probably not relevant. I'm confident that the problem isn't due to the file not existing. Thanks for your help guys I will do further research. – Bradley Thomas Aug 18 '14 at 15:55

1 Answers1

0

because aURL is nil here, before calling super init , you should check, if aURL is nill or not. if(aURL) will do it.

A file URL identifying the location in the application sandbox where document data is to be written. Passing in nil or an empty URL results in the throwing of an NSInvalidArgumentException.

actully the url you are providing in initWithFileURL is not a valid url, but a path. Don't' forget that a valid URL needs a scheme; for files, this is "file://". Try building the file URL using [NSURL fileURLWithPath:]

url = [NSURL fileURLWithPath:newFilePath];

convert it before calling initWithFileURL method.

Reference UIDocument

in case if aURL is valid url, may be just checking self will do the job

-(id)initWithFileURL:(NSURL *)url {
self = [super initWithFileURL:url];
if(self) {

}

return self;
}

Reference

Community
  • 1
  • 1
Pawan Rai
  • 3,434
  • 4
  • 32
  • 42
  • aURL is not nil, I've checked that different ways. Immediately before the crash line, it has an apparently valid value. I've also tried making aURL nil, that throws a different error. – Bradley Thomas Aug 18 '14 at 14:28
  • @BradThomas can you post the aURL, from debugger. – Pawan Rai Aug 18 '14 at 14:34
  • Ok, I have added that NSLog output to the question – Bradley Thomas Aug 18 '14 at 14:38
  • @BradThomas i was thinking correctly. actully the url you are providing in initWithFileURL is not a valid url. convert it to with [NSURL fileURLWithPath:here is path string] before calling initWithFileURL method. – Pawan Rai Aug 18 '14 at 14:41
  • I don't understand how that could be the problem, because a string in that format works fine in the legacy project. I've checked the value of aURL at the same point in the legacy project which does not crash, and aURL has the same format as the debug output provided in my question. My point is that I should not have to change the code like that. It seems to me it is likely related to config or setup (file system maybe?) I already know this code in my question, works perfectly in the legacy project. – Bradley Thomas Aug 18 '14 at 14:46
  • @BradThomas in case if aURL is valid url, may be just checking self will do the job -(id)initWithFileURL:(NSURL *)url { self = [super initWithFileURL:url]; if(self) { } return self; } – Pawan Rai Aug 18 '14 at 14:55