Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

trey5498

macrumors regular
Original poster
Jun 16, 2008
191
0
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.
 

HiRez

macrumors 603
Jan 6, 2004
6,265
2,630
Western US
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.
 

trey5498

macrumors regular
Original poster
Jun 16, 2008
191
0
the ";" is there I just forgot to translate it to the forums? Know where the error comes from?
 

yoavcs

macrumors regular
Apr 7, 2004
220
96
Israel
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.