NSArrayM insertObject:atIndex:]: object cannot be nil'

22,063

Solution 1

You haven't listed enough code in your question to definitely show where you're making an error.

Look in your code for any lines that say "insertObject:atIndex:". The object you're inserting is apparently nil.

If you can't find this line, add a symbolic breakpoint on the symbol "[NSArray insertObject:atIndex:]" (click the link to see specific instructions in an answer of a closely related question to yours) and see if you can break on it right before the crash happens.

Solution 2

I had a similar problem. My mistake was in not marking the view that I was trying to load as the initial view controller. My guess is that the there was no initial view controller to add to the array and hence the object was nil. I'm not sure how good this is at answering your question but it worked for me.

enter image description here

Share:
22,063
user2057209
Author by

user2057209

Updated on December 14, 2020

Comments

  • user2057209
    user2057209 over 3 years

    After a lot of search, and tries with some solutions on stackoverflow, i didn't find any answer which could solve my error : I have a UIViewcontroller, who's name is WorldViewController. In this UIViewcontroller, i had init some UIViews. I want to modificate some variables which depends of WorldViewController from some UIViews. And now that i put those lines :

    WorldViewController * World = [[WorldViewController alloc] init];
    

    it gives me :

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

    So, how to fix it ? Could it comes from the fact that WorldViewController is running ? And so, how to fix it ?

    Edit : init method of my viewcontroller

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
    
            World1 = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 480.0)];
            [World1 setBackgroundColor:[UIColor blackColor]];
            [self.view addSubview:World1];
            [World1 release];
    
            [self.view addSubview:ViewPig];
            [self.view addSubview:level1view];
            [self.view addSubview:level2view];
            [self.view addSubview:LoadView];
            [LoadView AnimPigLoadingWith:PigChargement];
            [LoadView AppearLabelLoading];
            [self.view addSubview:FondPauseGrise];
            [self.view addSubview:FondPause];
            [self.view addSubview:BoutonResume];
            [self.view addSubview:BoutonHome];
            [self performSelector:@selector(Chargement) withObject:nil afterDelay:0.5];
            [self performSelector:@selector(ViewAppears) withObject:nil afterDelay:5.5+2.5];
            [self performSelector:@selector(LabelPrevientDepartWithImage:) withObject:Trois afterDelay:9];
            [self performSelector:@selector(LabelPrevientDepartWithImage:) withObject:Deux afterDelay:10];
            [self performSelector:@selector(LabelPrevientDepartWithImage:) withObject:Un afterDelay:11];
            [self performSelector:@selector(LabelPrevientDepartWithImage:) withObject:Go afterDelay:12];
            [self performSelector:@selector(Lancement) withObject:nil afterDelay:(3)];
            [self performSelector:@selector(GestionMaps) withObject:nil afterDelay:(11+2)];
        }
        return self;
    }
    

    Thanks !