0
votes

I have this code:

if (self.leadObject != nil) {
    [result addObject:self.leadObject];
}

And sometimes app close with this error:

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

The execution pass if command and try to add the object self.leadObject inside of result array although this object is nil. Can someone explain this??

Update:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[__NSArrayM insertObject:atIndex:]: object cannot be nil' *** First throw call stack: (0x2ca8af87 0x3a1e9c77 0x2c9a6f29 0x1777ff 0x177a6f 0x177b7f 0x1779df 0x17446d 0xa1cdd 0xa7933 0x46cad3 0x46cabf 0x47051b 0x2ca50e61 0x2ca4f581 0x2c99cdb1 0x2c99cbc3 0x33d28051 0x2ff68a31 0xadb45 0x3a785aaf) libc++abi.dylib: terminating with uncaught exception of type NSException

1
are you sure the crash is caused at this part? I'd recommend adding a log statemenent before [result addObject:self.leadObject] where you print the value of self.leadObject, just to make sure... - nburk
Please paste a copy of the stack trace from the crash log into your question. - Phillip Mills
Are you sure you aren't calling insertObject:atIndex: somewhere? - Ian MacDonald
Please show the definition of the declared property leadObject if there is one, otherwise the ivar definition and the definition of the getter, if it is not synthesized. - Amin Negm-Awad
@FernandoGarcíaCorrochano I used another library, because I couldn't solve the problem - Roberto Gómez

1 Answers

-2
votes

I suspect that you have not allocated your result object. Do so if you didn't and then Log the self.leadObject to make sure that it isn't an NSNull

if (self.leadObject) {
    NSLog(@"%@", self.leadObject);
    [result addObject:self.leadObject];
}