Mano Chitra R
Mano Chitra R

Reputation: 241

exception in NSArrayM insertObject:atindex

i have used 4 images in project ..while running it results in:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[__NSArrayM insertObject:atIndex:]: object cannot be nil' *** First throw call stack:

my code :

NSArray *imageNames= @[@"jake_2.png",@"jake_3.png",@"jake_4.png",@"jake_5.png "];
// Do any additional setup after loading the view, typically from a nib.
NSMutableArray *images = [[NSMutableArray alloc] init];
for (int i = 0; i < imageNames.count; i++)
{
    [images addObject:[UIImage imageNamed:[imageNames objectAtIndex:i]]];
    UIImageView *slowAnimationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(160, 95, 86, 193)];
    slowAnimationImageView.animationImages = images;
    slowAnimationImageView.animationDuration = 5;
    [self.view addSubview:slowAnimationImageView];
    [slowAnimationImageView startAnimating];
}

Upvotes: 0

Views: 748

Answers (4)

Tejvansh
Tejvansh

Reputation: 676

You're facing issue because the imageName you provided in array is not available in the resources. Check the last object in array : @"jake_5.png ". There's an extra space in it. Please remove it. Thats what causing this issue.

UPDATE :

For animation, you need to set it after all images are added in your imageArray. Refer this code for help and make changes :

NSMutableArray *images = [[NSMutableArray alloc] init];
for (int i = 0; i < imageNames.count; i++)
{
    [images addObject:[UIImage imageNamed:[imageNames objectAtIndex:i]]];
}

slowAnimationImageView.animationImages = images;
slowAnimationImageView.animationDuration = 5;
[slowAnimationImageView startAnimating]; 

Hope it helps...

Upvotes: 4

Unheilig
Unheilig

Reputation: 16292

The problem arises because there is one extra space in @"jake_5.png ":

NSArray *imageNames= @[@"jake_2.png",@"jake_3.png",@"jake_4.png",@"jake_5.png "];

as it should be:

@"jake_5.png"

Addendum:

The way you want UIImageView to animate a series of images is not correct:

Please replace yours with the following:

UIImageView *slowAnimationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(160, 95, 86, 193)];
[self.view addSubview:slowAnimationImageView];

NSArray *imageNames = @[@"jake_2.png", @"jake_3.png", @"jake_4.png", @"jake_5.png"];

NSMutableArray *images = [[NSMutableArray alloc] init];
for (int i = 0; i < imageNames.count; i++)
{
    [images addObject:[UIImage imageNamed:[imageNames objectAtIndex:i]]];
}

slowAnimationImageView.animationImages = images;
slowAnimationImageView.animationDuration = 5;
[slowAnimationImageView startAnimating];

Upvotes: 0

Mahesh
Mahesh

Reputation: 996

NSArray *imageNames= @[@"jake_2.png",@"jake_3.png",@"jake_4.png",@"jake_5.png "]; replace it with 
NSArray *imageNames= @[@"jake_2.png",@"jake_3.png",@"jake_4.png",@"jake_5.png"];

because you put the space @"jake_5.png " may be this image was not available in resource so it give you error.

Upvotes: 0

Fatti Khan
Fatti Khan

Reputation: 1553

There is little mistake with the Space with name of the last image

 NSArray *imageNames= @[@"jake_2.png",@"jake_3.png",@"jake_4.png",@"jake_5.png"];
    // Do any additional setup after loading the view, typically from a nib.
    NSMutableArray *images = [[NSMutableArray alloc] init];
    for image in imagesNames
    {
        [images addObject:[image];
        UIImageView *slowAnimationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(160, 95, 86, 193)];
        slowAnimationImageView.animationImages = images;
        slowAnimationImageView.animationDuration = 5;
        [self.view addSubview:slowAnimationImageView];
        [slowAnimationImageView startAnimating];
    }

but if you don't want to add the for loop like this use the method addObjectOfArray to append the object in the Mutable Array

Upvotes: 1

Related Questions