Error with initWithObjects

Discussion in 'Mac Programming' started by trey5498, Jul 16, 2008.

  1. trey5498 macrumors regular

    Joined:
    Jun 16, 2008
    #1
    I need to know what is the error with this:

    Code:
    
    header file:
    
    NSArray *arrSelections1;
    
    
    .m file:
    
    -(id) init
    {
    	
    	if ([super init]) {
    		arrSelections1 = [[NSArray alloc] initWithObjects: cse211djq, arc118djq, cse211clq, arc118clq, hubatlabclq, nrng514clq, wei408clq, tur2215clq, arcatlabbwq, b105110bwq, cse211bwq, hubatlabbwq, lawpclabbwq, lbw3radbwq, nrnatlabbwq, tur2215bwq, weiatlabbwq, nil];
    			
    	}
    	return self;
    }
    -(void) dealloc
    {
    
    	[arrSelections1 release];
    	
    }
    
    
    - (IBAction)selectall:(id)sender 
    {
    	for(NSButton *button1 in arrSelections1) [button1 setState:NSOnState];
    	NSLog(@"%@", [arrSelections1 objectAtIndex: 5]);
    }
    
    error: 2008-07-16 11:03:39.403 Circa Printer Installer[6435:10b] *** -[NSCFArray objectAtIndex:]: index (5) beyond bounds (0)

    why it is not initializing and this is an array of buttons.
     
  2. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #2
    I'm not sure how that even compiled because you're missing a semicolon after your array declaration. Also you should be calling [super dealloc]; at the end of your dealloc method.

    If those objects are NSButtons you created in a nib/.xib (Interface Builder), they won't be valid in init, you'd have to put any code that works with them in awakeFromNib: instead, where they will be valid.
     
  3. trey5498 thread starter macrumors regular

    Joined:
    Jun 16, 2008
    #3
    the ";" is there I just forgot to translate it to the forums? Know where the error comes from?
     
  4. trey5498 thread starter macrumors regular

    Joined:
    Jun 16, 2008
    #4
    just reread your post and that was it.

    Thank you.
     
  5. yoavcs macrumors regular

    Joined:
    Apr 7, 2004
    Location:
    Israel
    #5
    Your NSButtons aren't yet initialized, therefore nil, so your array declaration in the -init method is basically like a [[NSArray alloc] initWithObjects:nil] therefore the array bounds are 0 and not 5.

    If you created those buttons and your custom class in the nib they won't be initialized when your -init method gets called.

    Move the creation of the array to the awakeFromNib: method.
     

Share This Page