Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Coredata error setObjectForKey: object cannot be nil

I am tryng to check if there is any data in my coredata storage as a type of recovery for my app. Basicly if the user is in the final view there is some data in coredata that they are constantly updating.

So they are in the final view then the app breaks or they put it to sleep then the app gets removed from memory.

when the app is next loaded I check my coredata object to see if there are any values If there are I prompt the user telling them there is unfinished work would you like to pick up from where you left off of continue fresh.

if they want to start fresh i dump anything thats currently in my core data and allow them to work. else I jump to the last view load up the data thats been in coredata and allow them to continue working.

However this is where the error happens I check my coredata like so.

NSMutableArray *checkFinMutableArray = [coreDataController readFin];

    if ([checkFinMutableArray count] > 0) {
        //Show mesage, recover or not?
        UIAlertView *alert = [[UIAlertView alloc] init];
        [alert setTitle:@"Selected projects avalible"];
        [alert setMessage:@"It appears that you have unfinished projects from a previous session. Would you like to continue working on these projects?"];
        [alert setDelegate:self];
        [alert addButtonWithTitle:@"Yes"];
        [alert addButtonWithTitle:@"No"];
        [alert show];
    }

this is what my coredata object looks like

 - (NSMutableArray *)readFinDimensions {
        NSManagedObjectContext *context = [self managedObjectContext];


        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"Project" inManagedObjectContext:context];
        [fetchRequest setEntity:entity];

        NSError *error;

        NSMutableArray *projectDictionaryArray = [[NSMutableArray alloc] init];


        NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];


        for (ProjectList *projectList in fetchedObjects) {

            NSMutableDictionary *tempProjectDictionaryArray = [[ NSMutableDictionary alloc] init];

            [tempProjectDictionaryArray setObject:project.proj forKey:@"Proj"]; // this is where the ap dies
            [tempProjectDictionaryArray setObject:project.desc forKey:@"Desc"];

            [projectDictionaryArray addObject:tempProjectDictionaryArray];
        }

        return projectDictionaryArray;
    }

and this is what the error looks like

 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** setObjectForKey: object cannot be nil (key: Proj)'

any help would be greatly appreciated.

like image 250
HurkNburkS Avatar asked Jan 30 '26 00:01

HurkNburkS


1 Answers

May be your code must be:

    for (ProjectList *projectList in fetchedObjects) {

        NSMutableDictionary *tempProjectDictionaryArray = [[ NSMutableDictionary alloc] init];

        [tempProjectDictionaryArray setObject:projectList.proj forKey:@"Proj"]; // this is where the ap dies
        [tempProjectDictionaryArray setObject:projectList.desc forKey:@"Desc"];

        [projectDictionaryArray addObject:tempProjectDictionaryArray];
    }

where project.proj is changed by projectList.proj (also for .desc).

like image 178
RFG Avatar answered Feb 01 '26 15:02

RFG



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!