I have a block of code which when executed gives me this error. And I am relatively new, I can’t seem to fix the problem.
Error:
2011-09-06 12:31:06.094 ForceGauge[266:707] CoreAnimation: ignoring exception: * -[NSMutableArray objectAtIndex:]: index 1 beyond bounds [0 .. 0]
-(void)peakCollector:(NSMutableArray *)testarray {
NSUInteger totalRows = [testarray count];
NSMutableArray *percentArray = [[NSMutableArray alloc]initWithObjects:0, nil];
if(forcecontroller.selectedSegmentIndex==0)
testarray = lbData;
else if(forcecontroller.selectedSegmentIndex==1)
testarray = kgData;
else if(forcecontroller.selectedSegmentIndex ==2)
testarray = ozData;
else if(forcecontroller.selectedSegmentIndex ==3)
testarray = newtonData;
for(int i = 0; i< totalRows-1; i++) {
if ([[testarray objectAtIndex:i+1] doubleValue] >= 1.2 * [[testarray objectAtIndex:i] doubleValue]) {
percentArray = [testarray objectAtIndex:i];
DatabaseTable *tableVC = [[DatabaseTable alloc] initWithStyle:UITableViewStylePlain];
[self.navigationController pushViewController:tableVC animated:YES];
if(forcecontroller.selectedSegmentIndex==0)
[tableVC copydatabase:percentArray];
else if(forcecontroller.selectedSegmentIndex==1)
[tableVC copydatabase:kgData];
else if(forcecontroller.selectedSegmentIndex==2)
[tableVC copydatabase:ozData];
else if(forcecontroller.selectedSegmentIndex==3)
[tableVC copydatabase:newtonData];
[tableVC release];
} else {
[analogData removeAllObjects];
}
}
}
There are multiple issues here:
1) NSArrays can only contains NSObjects.
In your code, you are initializing your NSArray using
[[NSMutableArray alloc]initWithObjects:0, nil];, but 0 is an atomic type, not an NSObject(and basically 0 is the same value as nil (nil and NULL are typically equal to 0, interpreted as the
idandvoid*types, respectively)You have to encapsulate your 0 value in an NSNumber instead :
Then retrieve the NSNumber using
[percentArray objectAtIndex:0]and finally convert back the retrieve NSNumber to int usingNSNumber‘sintValuemethod:2) The exception you got is in fact elsewhere and is much more subtle: you are retrieving the
[testarray count]value in anNSUIntegervariable, which is an unsigned type. ThentotalRows-1will do some tricky things if totalRows is equal to 0 (which is obviously the case considering the exception you have).As
totalRowsis anNSUInteger, when it is equal to 0,totalRows-1will not be equal to -1, but to…(NSUInteger)-1(-1 interpreted as an unsigned integer), which is 0xFFFFFFFF, or namely the maximum value of theNSUIntegertype!This is why
iis always less than thistotalRows-1value (as this value is not -1 but 0xFFFFFFFF = NSUInteger_MAX).To solve this issue, cast your
totalRowsvariable to an NSInteger value, or add a condition in your code to treat this special case separately.