Error says source path is nil

3.1k views Asked by At

I am quite new to iOS app development.

I am trying to build a sample DB app which saves records to DB and fetches from it.

The App works well when I test with simulators. I have created a local database and also I am programatically making a copy of it when required. Also I have checked that local DB is referenced in the 'Copy Bundle Resources'.

But the error I am getting is, " * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSFileManager copyItemAtPath:toPath:error:]: source path is nil' ".

I think the piece of code which causes this problem is " [FileManager copyItemAtPath:databasePathFromApp toPath:DBPath error:nil]; "

It work perfectly good for simulators but not when I test with my device.

Expecting help.

My code is,

- (id)init {

    self = [super init];

    //Datbase Name
    DBName=@"Person.sqlite3";

    //Getting DB Path
    NSArray *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentsDir = [documentsPath objectAtIndex:0];

    DBPath=[documentsDir stringByAppendingPathComponent:DBName ];
    //DBPath = [[NSString alloc] initWithString: [documentsDir stringByAppendingPathComponent:DBName]];
    NSLog(@"DBPath is  %@",DBPath);

    return self;

}


-(void)checkAndCreateDB{

    BOOL Success;

    //NSFileManager maintains File
    NSFileManager *FileManager = [NSFileManager defaultManager];
    NSLog(@"line 1");

    //Checks Database Path
    Success = [FileManager fileExistsAtPath:DBPath];
    NSLog(@"line 2");

    //If file exists, it returns true
    if(Success)return;
    NSLog(@"line 3");

    //NSString *databasePathFromApp = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:DBName];

    // Get the path to the database in the application package
    NSString *databasePathFromApp=[[NSBundle mainBundle] pathForResource:@"Person.sqlite3" ofType:@"sqlite3"];
    NSLog(@"line 4");

    [FileManager copyItemAtPath:databasePathFromApp toPath:DBPath error:nil];
    //[FileManager release];
    NSLog(@"line 5");

}

My Application crashes while testing in device after the line NSLOG(line 4); But works good in simulator.

Many Thanks Prakash

2

There are 2 answers

1
zaph On

The device can not write to the app bundle, you need to use the Documents directory.

Here is how to get the Documents directory path:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName];

If you have an initial db in the app bundle copy it to the Documents on initial startup.

2
X Slash On

change this line

NSString *databasePathFromApp=[[NSBundle mainBundle] pathForResource:@"Person.sqlite3" ofType:@"sqlite3"];

to

NSString *databasePathFromApp=[[NSBundle mainBundle] pathForResource:@"Person" ofType:@"sqlite3"];

also, pay attention to capitalisation, iOS devices are case sensitive, while on simulator, you'll get away with wrong capitalisation