title: Core Data Crash: [__NSArrayM insertObject:atIndex:]: object cannot be nil
body: <p>I am trying to implement CoreData in my application to store a small database. </p>
  
  <p>Here my implementation:</p>
  
  <p>AppDelegate.h</p>
  
  <pre><code>#import &lt;UIKit/UIKit.h&gt;
  #import "FavoritosViewController.h"
  #import &lt;CoreData/CoreData.h&gt;
  
  @interface XXX : NSObject &lt;UIApplicationDelegate&gt;{
  
      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
  </code></pre>
  
  <p>AppDelegate.m</p>
  
  <pre><code>    - (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] &amp;&amp; ![managedObjectContext save:&amp;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:&amp;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];
  }
  </code></pre>
  
  <p>I have also a xcdatamodeld with "Event" entity with theirs attributes, and Event.h, Event.m from it.</p>
  
  <p>In FavoritosViewController I have also all the methods, but the problem comes before all that.</p>
  
  <p>It comes at </p>
  
  <pre><code>managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];    
  </code></pre>
  
  <p>The app crash and it appears the following:</p>
  
  <blockquote>
    <p><strong>* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*</strong> -[__NSArrayM
    insertObject:atIndex:]: object cannot be nil'</p>
  </blockquote>
  
  <p>Any ideas??? Thanks!!!</p>