Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I am getting a warning when run an analyse in xcode in my app.

Argument to 'NSMutableArray' method 'addObject:' cannot be nil.

I am getting the error when I try to add an object to an array. Where am I going wrong?

while(sqlite3_step(statement) == SQLITE_ROW)
{
NSLog(@"sqlite3_step");
imageUrlField = ((char *)sqlite3_column_text(statement, 0))?
[NSString  stringWithUTF8String
(char*)sqlite3_column_text(statement,0)]:
nil;

 imageTitleField = ((char*)sqlite3_column_text(statement,1)) ?
 [NSString stringWithUTF8String:
 (char*)sqlite3_column_text(statement,1)] :
 nil;

 lat = ((char *)sqlite3_column_text(statement,2)) ?
  [NSString stringWithUTF8String:
  (char *)sqlite3_column_text(statement,2)] :
  nil;

 lon = ((char *)sqlite3_column_text(statement,3)) ?
 [NSString stringWithUTF8String:  
 (char*)sqlite3_column_text(statement,3)]:
 nil;

 id1 = ((char *)sqlite3_column_text(statement,4)) ?
 [NSString stringWithUTF8String:
 (char   *)sqlite3_column_text(statement,4)] :
            nil;

 visited = ((char *)sqlite3_column_text(statement,5)) ?
 [NSString stringWithUTF8String:
  (char *)sqlite3_column_text(statement,5)] :
            nil;
            if(imageUrlField == nil)
            {
                NSLog(@"null");

            } else
            {
                [imageAndTitleArray addObject:imageUrlField];
                [imageAndTitleArray addObject:imageTitleField];
                [imageAndTitleArray addObject:lat];
                [imageAndTitleArray addObject:lon];
                [imageAndTitleArray addObject:id1];
                [imageAndTitleArray addObject:visited];

              // NSLog(@"url:%@",imageUrlField);
             //  NSLog(@"title:%@",imageTitleField);
            //  NSLog(@"lat:%@",lat);
             //  NSLog(@"lon:%@",lon);
             //  NSLog(@"lon:%@",id1);
            }

        }
share|improve this question
    
there are meny values can be nil, according to your code... I would replace the [... addObject:<value>]; pattern with [... addObject:<value>?:@""]; as you'd put string anyway into the array, so like e.g. a [imageAndTitleArray addObject:lat?:@""]; – holex Apr 21 '15 at 7:52

You can not add nil in an NSMutableArray, so in case your string instances are nil and you are trying to add them in NSMutableArray, you will get the above error of API Misuse (API MISUSE(APPLE) Argument to 'NSMutableArray' method 'addObject:' cannot be nil).

You should check if the string is not nil before adding it to the

NSMutableArray as

if(imageUrlField != nil)
                [imageAndTitleArray addObject:imageUrlField];

if(imageTitleField != nil)
                [imageAndTitleArray addObject:imageTitleField];

if(lat != nil)
                [imageAndTitleArray addObject:lat];

//like so for others

if(visited != nil)
                [imageAndTitleArray addObject:visited];

If for any case you need to add null in NSMutableArray than you can use [array addObject:[NSNull null]];

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.