Mick MacCallum
Mick MacCallum

Reputation: 130222

Simulator crashes "[NSMutableArray insertObject:atIndex:]: attempt to insert nil object at 0' "

I am working on an application that will utilize a custom image picker and try as I might, I can not seem to get the application to run quite right. Xcode debugger flags the following "Thread 1: Program recieved signal: "SIGABRT"."

  - (id) init { 
    if ((self = [super init])) {
        _images =  [[NSMutableArray alloc] init];
        _thumbs =  [[NSMutableArray alloc] init];
    }
    return self;
}

- (void)addImage:(UIImage *)image {
    [_images addObject:image];
    [_thumbs addObject:[image imageByScalingAndCroppingForSize:CGSizeMake(64, 64)]];
}

This is in xcode 4 on the new debugger. Thanks in advance.

Upvotes: 0

Views: 1543

Answers (1)

Alan Zeino
Alan Zeino

Reputation: 4396

One of those objects is nil. The following code will help you discover which one:

- (void)addImage:(UIImage *)image 
{
    if (image)
    {
        [_images addObject:image];
    }
    else
    {
        NSLog(@"image is nil");
    }

    UIImage *newImage = [image imageByScalingAndCroppingForSize:CGSizeMake(64, 64)];
    if (newImage)
    {
        [_thumbs addObject:newImage];
    }
    else
    {
        NSLog(@"newImage is nil");
    }
}

Upvotes: 5

Related Questions