9

In my iOS app I'm trying to update user information in the database (with Stackmob), but I keep getting "unrecognized selector sent to instance."

- (IBAction)save:(UIButton *)sender {

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"User"];
NSPredicate *predicte = [NSPredicate predicateWithFormat:@"username == %@", self.username];
[fetchRequest setPredicate:predicte];

[self.managedObjectContext executeFetchRequest:fetchRequest onSuccess:^(NSArray *results) {

    NSManagedObject *todoObject = [results objectAtIndex:0];
    [todoObject setValue:@"example@gmail.com" forKey:@"email"];

    [self.managedObjectContext saveOnSuccess:^{
        NSLog(@"You updated the todo object!");
    } onFailure:^(NSError *error) {
        NSLog(@"There was an error! %@", error);
    }];

} onFailure:^(NSError *error) {

    NSLog(@"Error fetching: %@", error);

}];
}

Here's the full error I'm getting:

-[NSNull length]: unrecognized selector sent to instance 0x1ec5678

2013-07-21 12:01:25.773 [29207:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', 
reason: '-[NSNull length]: unrecognized selector sent to instance 0x1ec5678'

Thanks in advance.

sambol
  • 194
  • 1
  • 2
  • 12
  • 1
    some where you getting Nil value or object and you try to set nil that why you got this error. check befor setValue with it's nil or not. – Nitin Gohel Jul 22 '13 at 05:18
  • possible duplicate of [-\[NSNull length\]: unrecognized selector sent to... A memory leak?](http://stackoverflow.com/questions/16607960/nsnull-length-unrecognized-selector-sent-to-a-memory-leak) – borrrden Jul 22 '13 at 05:47
  • Could you print self.username and see what it contains? – Satheesh Jul 22 '13 at 05:52

1 Answers1

42

I think it may because your self.username is empty. Note that if you are getting the username data from json , you can't use if(username){...} but

if(![username isKindOfClass:[NSNull class]])

to avoid empty data because the json interpreter will generate an NSNull object.

Pawan Sharma
  • 3,199
  • 1
  • 25
  • 32
Chen Xiaofeng
  • 496
  • 3
  • 8
  • 2
    Although this solution works but I have found a better solution here : http://stackoverflow.com/questions/16607960/nsnull-length-unrecognized-selector-sent-to-a-memory-leak/16610117 – Pawan Sharma Apr 10 '14 at 07:20
  • @पवन While that may work in certain circumstances, it's a pretty dangerous strategy to use in production. – dmur May 20 '16 at 16:39
  • but what does JSON have to do with this? – user102008 Jan 12 '17 at 19:22