Question

What can I do to avoid the app crashing when it encounters a null value?

the error message I get is:

-[NSNull isEqualToString:]: unrecognized selector sent to instance.

I tried this conditional statement to check for a null value, but it still crashes. listingWebAddress is a NSString.

 if (listingWebAddress == nil)
    {
        [webLabel setText:@""];

    } else {

    [webLabel setText:listingWebAddress];

    }

it works fine when a "listingWebAddress" exists.

thanks for the help :)

Update:

thanks to The Tiger's response the code now works. The solution was:

 if (![listingWebAddress isKindOfClass:[NSNull class]])
    {
        // do your task here

        [webLabel setText:listingWebAddress];

    } else {

        [webLabel setText:@"no web url"];

    }
Was it helpful?

Solution

1. If it is NSString you can check its length and if it is an NSArray you check its count.

2. You can simply put it in if condition, condition will return YES only in case of the object is not nil. Example:

if (object)
{
    //do your task here
}

3. In Objective-C You can check it by its class.

if (![object isKindOfClass:[NSNull class]])
{
   // do your task here
}

The NSNull class defines a singleton object used to represent null values in collection objects (which don’t allow nil values).

OTHER TIPS

It looks like listingWebAddress is already NSNull, which doesn't support method mutableCopy. I would change condition to:

if ( [listingWebAddress isKindOfClass: [NSNull class]] == NO){
  ... rest of logic 
}

try this

if(listingWebAddress !=[NSNull null]
{
   //your code
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top