Question

i have a view controller to add a core data record. The core data entity name is FavoriteThings, the attribute is thingname. I have a save button action called SaveButtonAction. When I tap inside the button, the text inserted in the textfield called ToDoTextField should be stored, but the app crashed showing following log error:

2013-12-09 12:30:07.488 Favorite Things[1701:a0b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'FavoriteThing'' 

This is the code for the method

- (IBAction)SaveButtonAction:(id)sender {
    FavoriteThing *newEntry = [NSEntityDescription insertNewObjectForEntityForName:@"FavoriteThing" inManagedObjectContext:managedObjectContext ];
    newEntry.thingName = self.ToDoTextField.text;
    NSError *error;
    if (![self.managedObjectContext save:&error])
    {
        NSLog(@"Whoops, couldn't save:%@",[error localizedDescription]);
    }

Thank you for your time..

Was it helpful?

Solution 3

Please check the enity name and also do the following

in YourAppDeleagte.h

+(YourAppDeleagte*)sharedManagedContext;

in YourAppDeleagte.m

  +(YourAppDeleagte*)sharedManagedContext{

     return (YourAppDeleagte *)[[UIApplication sharedApplication]delegate];
}

in viewController.m

#import "YourAppDelegate.h"

@property(nonatomic,retain)NSmanagedObjectContext *managedObjectContext;

-(void)viewDidLoad{
   [super viewDidLoad];
   self.managedObjectContext=[YourAppDelagete shareManagedContext].managedObjectContext;         
 }

OTHER TIPS

You don't pass your NSManagedObjectContext to the view controller (your context is nil).
Try keeping a strong reference to it and initialise your view controller with a valid context.

If you use the boilerplate code of a CoreData project, you could get access to the main context via your app delegate: appDelegate.managedObjectContext

It's telling you that your managedObjectContext parameter has the value of nil. Perhaps you meant [self managedObjectContext], which I am going to guess is an accessor that may be "lazy" instantiating the managed object context, and at this point hasn't been called yet. You are accessing the instance variable directly in the code that is throwing an exception.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top