0

Crash :

[NSNull boolValue]: unrecognized selector sent to instance 0x1b5b12878 2018-05-13 19:04:46.024492+0530 Doctor Express Provider[1681:579933] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull boolValue]: unrecognized selector sent to instance 0x1b5b12878

Code :

NSUserDefaults *pref=[NSUserDefaults standardUserDefaults];

strUserId = [pref valueForKey:PARAM_ID];
strUserToken = [pref valueForKey:PARAM_TOKEN];

NSMutableDictionary *dictparam=[[NSMutableDictionary alloc]init];
[dictparam setObject:strUserId forKey:PARAM_ID];
[dictparam setObject:strUserToken forKey:PARAM_TOKEN];
[dictparam setObject:struser_longi forKey:PARAM_LONGITUDE];
[dictparam setObject:struser_lati forKey:PARAM_LATITUDE];
[dictparam setObject:strRequsetId forKey:PARAM_REQUEST_ID];
[dictparam setObject:@"0" forKey:PARAM_DISTANCE];
AFNHelper *afn=[[AFNHelper alloc]initWithRequestMethod:POST_METHOD];

[afn getDataFromPath:FILE_WALK_LOCATION withParamData:dictparam withBlock:^(id response, NSError *error)
{

    NSLog(@"Update Walk Location = %@",response);
    if (response)
    {
        payment=[[response valueForKey:@"payment_type"] boolValue];
        NSLog(@"Something To Print");
        NSLog(@"%d", payment);
        NSLog(@"Something To Print");

        if (payment==0)
        {
            self.lblPayment.text = NSLocalizedString(@"CARD", nil);
            self.imgCashCard.image = [UIImage imageNamed:@"card"];
        }
        else if (payment==1)
        {
            self.lblPayment.text = NSLocalizedString(@"CASH", nil);
            self.imgCashCard.image = [UIImage imageNamed:@"cash"];

        }
        if([[response valueForKey:@"success"] intValue]==1)
        {
            totalDist=[[response valueForKey:@"distance"]floatValue];
            [self.btnDistance setTitle:[NSString stringWithFormat:@"%.2f %@",[[response valueForKey:@"distance"] floatValue],[response valueForKey:@"unit"]] forState:UIControlStateNormal];
        }
        else
        {
            [self.btnDistance setTitle:[NSString stringWithFormat:@"%.2f %@",[[response valueForKey:@"distance"] floatValue],[response valueForKey:@"unit"]] forState:UIControlStateNormal];

            if ([[response valueForKey:@"is_cancelled"] integerValue]==1)
            {
                [self.timerForCancelRequest invalidate];
                [self.timerForDestinationAddr invalidate];
                NSUserDefaults *pref=[NSUserDefaults standardUserDefaults];
                [pref removeObjectForKey:PREF_REQUEST_ID];
                [pref removeObjectForKey:PREF_NAV];
                strRequsetId=[pref valueForKey:PREF_REQUEST_ID];


                [APPDELEGATE showToastMessage:NSLocalizedString(@"Request Canceled", nil) ];


                is_walker_started=0;
                is_walker_arrived=0;
                is_started=0;
                [btnWalker setHidden:NO];
                [btnArrived setHidden:YES];
                [btnWalk setHidden:YES];

                [self.navigationController popToRootViewControllerAnimated:YES];

            }
        }
    }

}];

Error on this line :

NSLog(@"Update Walk Location = %@",response);

"dest_latitude" = 0;
"dest_longitude" = 0;
error = "Service not yet started";
"error_code" = 414;
"is_cancelled" = 0;
"payment_type" = "";
success = 0;
unit = kms;

Getting the null for payment_type...but getting crash on this line

payment=[[response valueForKey:@"payment_type"] boolValue];

Dot know how to handle that. But this print function itself not printing :

NSLog(@"Something To Print");
NSLog(@"%d", payment);
NSLog(@"Something To Print");

Any idea on how to handle this?

Updated :

  if([response valueForKey:@"payment_type"] != nil || ![[response valueForKey:@"payment_type"] isEqualToString:@""]){
                         payment=[[response valueForKey:@"payment_type"] boolValue];
                         NSLog(@"Something To Print");
                         NSLog(@"%d", payment);
                         NSLog(@"Something To Print");

                         if (payment==0)
                         {
                             self.lblPayment.text = NSLocalizedString(@"CARD", nil);
                             self.imgCashCard.image = [UIImage imageNamed:@"card"];
                         }
                         else if (payment==1)
                         {
                             self.lblPayment.text = NSLocalizedString(@"CASH", nil);
                             self.imgCashCard.image = [UIImage imageNamed:@"cash"];

                         }
                        }
  • @Moritz that only, i dont know hoe to handle that..can u please help me out – kiran kumar May 13 '18 at 14:09
  • 1
    Possible duplicate of [Checking a null value in Objective-C that has been returned from a JSON string](https://stackoverflow.com/questions/4839355/checking-a-null-value-in-objective-c-that-has-been-returned-from-a-json-string) – Larme May 13 '18 at 14:16
  • @Larme i checked that..but not able to check the null type before pass the value...but i can gett the values of payment as 0 or 1 – kiran kumar May 13 '18 at 14:17
  • If you have contact with the web developer, using a bool to determine payment if it's by cash or credit card, that strange. Because in your case, you have a third value: "unknown" (which is set as null). Would have been better a "enum" or a string, or anything else. – Larme May 13 '18 at 14:17
  • its not possible to contact, so only i needs to do in my code..please ..help me out..htting my head for past 1hr – kiran kumar May 13 '18 at 14:18
  • "but not able to check the null type before pass the value". That's false. `payment=[[response valueForKey:@"payment_type"] boolValue];` => Is translation for `NSNumber *paymentAsNumber = [response valueForKey:@"payment_type"]; payment = [paymentAsNumber boolValue];` It's just that you are doing two calls in one line, and that's where you need to learn to separate. So check `paymentAsNumber`. – Larme May 13 '18 at 14:46
  • @Larme it is possible to handle in code mannully ?? – kiran kumar May 13 '18 at 14:48
  • Adapting from linked question and with code from previous comment: `if (paymentAsNumber == (id)[NSNull null]){payment = NO?YES?Don'tKnowwhatIsDefaultValue} else {payment = [paymentAsNumber boolValue];` – Larme May 13 '18 at 14:49
  • @ Larme i am very much confused..if u give me code solution with my question code its would be helpful..i can learn from that..i am new bie to this – kiran kumar May 13 '18 at 14:56
  • You need to find out what instance (0x1b5b12878) is causing the crash. See eg here: https://stackoverflow.com/questions/3403806/how-to-find-instance-by-hex-in-xcode-console – koen May 13 '18 at 21:48

1 Answers1

0

Kiran, you can check whether payment_type is null before you get boolValue for it.

NSUserDefaults *pref=[NSUserDefaults standardUserDefaults];

strUserId = [pref valueForKey:PARAM_ID];
strUserToken = [pref valueForKey:PARAM_TOKEN];

NSMutableDictionary *dictparam=[[NSMutableDictionary alloc]init];
[dictparam setObject:strUserId forKey:PARAM_ID];
[dictparam setObject:strUserToken forKey:PARAM_TOKEN];
[dictparam setObject:struser_longi forKey:PARAM_LONGITUDE];
[dictparam setObject:struser_lati forKey:PARAM_LATITUDE];
[dictparam setObject:strRequsetId forKey:PARAM_REQUEST_ID];
[dictparam setObject:@"0" forKey:PARAM_DISTANCE];
AFNHelper *afn=[[AFNHelper alloc]initWithRequestMethod:POST_METHOD];

[afn getDataFromPath:FILE_WALK_LOCATION withParamData:dictparam withBlock:^(id response, NSError *error)

{

NSLog(@"Update Walk Location = %@",response);
if (response)
{
    if([response valueForKey:@"payment_type"] != nil && ![[response valueForKey:@"payment_type"] isEqualToString: ""])
    {
        payment=[[response valueForKey:@"payment_type"] boolValue];
        NSLog(@"Something To Print");
        NSLog(@"%d", payment);
        NSLog(@"Something To Print");

        if (payment==0)
        {
            self.lblPayment.text = NSLocalizedString(@"CARD", nil);
            self.imgCashCard.image = [UIImage imageNamed:@"card"];
        }
        else if (payment==1)
        {
            self.lblPayment.text = NSLocalizedString(@"CASH", nil);
            self.imgCashCard.image = [UIImage imageNamed:@"cash"];

        }
        if([[response valueForKey:@"success"] intValue]==1)
        {
            totalDist=[[response valueForKey:@"distance"]floatValue];
            [self.btnDistance setTitle:[NSString stringWithFormat:@"%.2f %@",[[response valueForKey:@"distance"] floatValue],[response valueForKey:@"unit"]] forState:UIControlStateNormal];
        }
        else
        {
            [self.btnDistance setTitle:[NSString stringWithFormat:@"%.2f %@",[[response valueForKey:@"distance"] floatValue],[response valueForKey:@"unit"]] forState:UIControlStateNormal];

            if ([[response valueForKey:@"is_cancelled"] integerValue]==1)
            {
                [self.timerForCancelRequest invalidate];
                [self.timerForDestinationAddr invalidate];
                NSUserDefaults *pref=[NSUserDefaults standardUserDefaults];
                [pref removeObjectForKey:PREF_REQUEST_ID];
                [pref removeObjectForKey:PREF_NAV];
                strRequsetId=[pref valueForKey:PREF_REQUEST_ID];


                [APPDELEGATE showToastMessage:NSLocalizedString(@"Request Canceled", nil) ];


                is_walker_started=0;
                is_walker_arrived=0;
                is_started=0;
                [btnWalker setHidden:NO];
                [btnArrived setHidden:YES];
                [btnWalk setHidden:YES];

                [self.navigationController popToRootViewControllerAnimated:YES];

            }
        }
    }
}

}];

Hope this is what you want.

Prajakta
  • 322
  • 1
  • 15
  • @prakakta wr i needs to add..like after else statement – kiran kumar May 13 '18 at 14:30
  • check my post..please..getting same again – kiran kumar May 13 '18 at 14:41
  • @prakakta [NSNull isEqualToString:]: unrecognized selector sent to instance 0x1b5b12878 2018-05-13 20:16:22.817669+0530 Doctor Express Provider[1750:603892] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull isEqualToString:]: unrecognized selector sent to instance 0x1b5b12878' – kiran kumar May 13 '18 at 14:47
  • i chnage the line to lie ` if([response valueForKey:@"payment_type"] != nil && ![response valueForKey:@"payment_type"])` but why this is not printing n my console` NSLog(@"Something To Print"); NSLog(@"%d", payment); NSLog(@"Something To Print");` – kiran kumar May 13 '18 at 14:53
  • Because payment_type is not set it is nil so this is not printing something to print – Prajakta May 14 '18 at 09:00