Kilij522
Kilij522

Reputation: 3

NSInvalidArgumentException: object cannot be nil

I am relatively new to Xcode and am trying to write an app that will play a video from iTunes from a tutorial but my app keeps crashing.

It is giving me the error code:

'NSInvalidArgumentException', reason: '* -[__NSArrayM insertObject:atIndex:]: object cannot be nil'

I used breakpoints and found where the problem is but I'm not sure how to fix it. The line that is giving me problems is:

[list addObject:[item valueForProperty:MPMediaItemPropertyAssetURL]]; 

Does anyone have any ideas? Below is the .m file code:

@implementation iPodLibrary

+(NSArray *) movieList {

    NSMutableArray *list =

    [[[NSMutableArray allocWithZone:NULL] init] autorelease];

    MPMediaQuery *query = [[MPMediaQuery alloc] init];

    [query addFilterPredicate:[MPMediaPropertyPredicate

                               predicateWithValue:[NSNumber numberWithInteger:(MPMediaTypeAny ^ MPMediaTypeAnyAudio)]
                               forProperty:MPMediaItemPropertyMediaType]];

    for (MPMediaItem* item in [query items]) {
        [list addObject:[item valueForProperty:MPMediaItemPropertyAssetURL]];
    }
    [query release];

    return list;    
}

+ (NSString *) movieTitle:(NSURL *)aURL {

    NSString *aTitle = nil;

    if (aURL) {

        AVAsset* aAsset = [[AVURLAsset allocWithZone:NULL]
                           initWithURL:aURL options:nil];

        for (AVMetadataItem* metadata in [aAsset commonMetadata]) {

            if ([[metadata commonKey] isEqualToString:AVMetadataCommonKeyTitle]) {

                aTitle = [metadata stringValue];

                break;              
            }
        }       
    }

    return aTitle;    
}

@end

Upvotes: 0

Views: 1410

Answers (2)

deleted_user
deleted_user

Reputation: 3805

In Objective C you cannot put nil values into NSArray or NSDictionary.

Upvotes: 0

aj.esler
aj.esler

Reputation: 931

Check that [item valueForProperty:MPMediaItemPropertyAssetURL] is actually returning a value. NSMutableArray does not allow the addition of a nil value with the addObject method. See NSMutableArray addObject: documentation

You can check if it is null like so:

for (MPMediaItem* item in [query items]) {
    id itemValue = [item valueForProperty:MPMediaItemPropertyAssetURL];
    [list addObject:itemValue]; // break point this line and see what itemValue is.
}

Upvotes: 1

Related Questions