-1

I have an error message ' Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: ' -[__NSArrayM insertObject:atIndex:]: object cannot be nil'

The problem is on the fetchedObjects.

This is because it tries to add a value that is nil to an NSArray. What I don't understand, is why there is that empty value.

For info: The table is the name of the table of my sqlite and the label is just a field of my table.

- (NSMutableArray*) getLabel:(NSString *)label fromTable:(NSString *)table 
{
    NSMutableArray * ret = [NSMutableArray new];
    NSError *error = nil;
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:table
                                              inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];
    NSArray *fetchedObjects = [managedObjectContext executeFetchRequest:fetchRequest error:&error];

    Class theClass = NSClassFromString(table);
    id info = [[theClass alloc] initWithEntity:entity insertIntoManagedObjectContext:managedObjectContext];

    for (info in fetchedObjects)
    {
        [ret addObject:[info valueForKey: label]];
    }
    return ret;
}

Good initialization:
enter image description here

Bad initialization:
enter image description here

Do you have any ideas? Thanks in advance.

ΩlostA
  • 2,501
  • 5
  • 27
  • 63
  • Nothing surprising, that just now allocated object have empty fields. – Cy-4AH Sep 17 '18 at 14:57
  • 1
    You should always check if an object is nil before adding it to an array or dictionary. There is not enough info there to understand why [info valueForKey: label] is nil. We would need to see the piece of code that creates your managed object and stores it in the database. We would also need to know the value of 'label'. – Dimitar08 Sep 17 '18 at 15:50
  • @Cy-4AH I didn't understand, what do you mean? – ΩlostA Sep 17 '18 at 16:14
  • @Dimitar08 I know that I have to check the nil, but my problem is not to avoid the nil, but to explain it. I don't want to hide the dust under the carpet... I will put more information in the ticket if it can help. – ΩlostA Sep 17 '18 at 16:16
  • After taking a closer look at the code, Cy-4AH is correct. Your code is creating a new managed object. Makes sense that none of the fields on the object would be populated with data at this point. – Dimitar08 Sep 17 '18 at 21:40
  • @Dimitar08 For info, I updated my ticket, I added For info: The table is the name of the table of my sqlite and the label is just a field of my table. I confirm you that at this point, I have some fields in my object. The table contains the fields label1, label2, label3, etc.... And I give it the parameter label that is "label1", that is a field in the table. It always worked until today, I don't understand why it suddenly accumulate some nil datas... – ΩlostA Sep 18 '18 at 10:04

1 Answers1

1

It is a common error. I think you should have a look at your line:

id info = [[theClass alloc] initWithEntity:entity insertIntoManagedObjectContext:managedObjectContext];

Look here if you want to have more info: Use NSManagedObject class without initWithEntity:?

You should insertIntoManagedObjectContect:nil

dt dino
  • 1,194
  • 6
  • 19
  • Ohh thank you very much, I was looking everywhere, and it was so simple! Many thanks, you save my week :D (it added lines and lines just because of that. It is very strange that for initializing a variable, I need to use an insertIntoMAnagedObjectContext..., because I tried the simple initWithEntity: and it didn't work) – ΩlostA Sep 18 '18 at 12:10
  • 1
    I think he need just `for (id info in fetchedObjects)` without any `insertIntoManagedObjectContect` – Cy-4AH Sep 19 '18 at 09:56