kiran kumar
kiran kumar

Reputation: 1

NSNull boolValue getting crash objective c

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"];

                         }
                        }

Upvotes: 0

Views: 906

Answers (1)

Prajakta
Prajakta

Reputation: 322

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.

Upvotes: 0

Related Questions