hanumanDev
hanumanDev

Reputation: 6614

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

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

    }

Upvotes: 0

Views: 1396

Answers (3)

TheTiger
TheTiger

Reputation: 13354

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).

Upvotes: 1

Aman Aggarwal
Aman Aggarwal

Reputation: 3754

try this

if(listingWebAddress !=[NSNull null]
{
   //your code
}

Upvotes: 0

The Tosters
The Tosters

Reputation: 417

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 
}

Upvotes: 0

Related Questions