Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core Data Crash: [__NSArrayM insertObject:atIndex:]: object cannot be nil

I am trying to implement CoreData in my application to store a small database.

Here my implementation:

AppDelegate.h

#import <UIKit/UIKit.h>
#import "FavoritosViewController.h"
#import <CoreData/CoreData.h>

@interface XXX : NSObject <UIApplicationDelegate>{

    NSManagedObjectModel *managedObjectModel;
    NSManagedObjectContext *managedObjectContext;       
    NSPersistentStoreCoordinator *persistentStoreCoordinator;



}
- (NSString *)applicationDocumentsDirectory;

@property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
@property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;

@end

AppDelegate.m

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{

    FavoritosViewController *global=[[FavoritosViewController alloc]init];

    global.managedObjectContext=[self managedObjectContext];

    .
    .
    .
    }

        - (void)applicationWillTerminate:(UIApplication *)application
    {
        NSError *error = nil;
        if (managedObjectContext != nil) {
            if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {
                /*
                 Replace this implementation with code to handle the error appropriately.

                 abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
                 */
                NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
                abort();
            } 
        }

    }

- (NSManagedObjectContext *) managedObjectContext {

    if (managedObjectContext != nil) {
        return managedObjectContext;
    }

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil) {
        managedObjectContext = [[NSManagedObjectContext alloc] init];
        [managedObjectContext setPersistentStoreCoordinator: coordinator];
    }
    return managedObjectContext;
}


/**
 Returns the managed object model for the application.
 If the model doesn't already exist, it is created by merging all of the models found in the application bundle.
 */
- (NSManagedObjectModel *)managedObjectModel {

    if (managedObjectModel != nil) {
        return managedObjectModel;
    }
    managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];    
    return managedObjectModel;
}


/**
 Returns the persistent store coordinator for the application.
 If the coordinator doesn't already exist, it is created and the application's store added to it.
 */
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {

    if (persistentStoreCoordinator != nil) {
        return persistentStoreCoordinator;
    }

    NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"FavoritosDatabase.sqlite"]];

    NSError *error = nil;
    persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error]) {
        /*
         Replace this implementation with code to handle the error appropriately.

         abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.

         Typical reasons for an error here include:
         * The persistent store is not accessible
         * The schema for the persistent store is incompatible with current managed object model
         Check the error message to determine what the actual problem was.
         */
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }    

    return persistentStoreCoordinator;
}




- (NSString *)applicationDocumentsDirectory {
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}

I have also a xcdatamodeld with "Event" entity with theirs attributes, and Event.h, Event.m from it.

In FavoritosViewController I have also all the methods, but the problem comes before all that.

It comes at

managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];    

The app crash and it appears the following:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[__NSArrayM insertObject:atIndex:]: object cannot be nil'

Any ideas??? Thanks!!!

like image 291
saimonx Avatar asked Nov 01 '11 12:11

saimonx


3 Answers

I had a similar problem with the same error message when my code ran [NSManagedObjectModel mergedModelFromBundles:nil]. This happened after xCode crashed on me unexpectedly. Even when I reverted back to a known good version of my code, I still had the same error that was due to some sort of corruption.

After much experimenting, I was able to solve the problem by exiting Xcode and the iPhone simulator, and then deleting all files from the following directories:

$ cd /Users/john/Library/Developer/Xcode/DerivedData

$ rm -R -f ./(folder corresponding to my project name)

$ cd /Users/john/Library/Application Support/iPhone Simulator/5.0/Applications

$ rm -R *

This was able to clear the corrupted temporary files and state for the simulator, and the error disappeared.

like image 84
jcanfield55 Avatar answered Sep 28 '22 01:09

jcanfield55


Had a similar error. When you first create the model in Xcode, it seems to store the name of the model internally.

I renamed the model file - this caused the problem mentioned by OP.

Reverting the model name back (and doing a build clean + deleting the app form the device) fixed the issue for me.

like image 31
BastiBen Avatar answered Sep 28 '22 03:09

BastiBen


I moved my Model.xcdatamodeld file to another folder, and got this error. Starting with a clean emulator didn't help. Apparently Xcode keeps a reference to this file somewhere.

My fix was to backup my old Model.xcdatamodeld file, remove it from the project, create a new model file in the same folder and then replace this file with the backup.

like image 28
Jaap van Hengstum Avatar answered Sep 28 '22 01:09

Jaap van Hengstum