Reputation: 1
I am trying to create a quiz app. My code should load the questions & answers from a CSV and then display them randomly. On the simulator, it works well up until at least question 85. After which it starts throwing a SIGABRT
error:
2011-12-27 19:03:22.026 100Q_Capitals[5369:207] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSMutableArray objectAtIndex:]: index 99 beyond bounds [0 .. 95]'
Here's the code that I have used:
Both questionTota
l and questionCount100
are initialized as int, and then to set up the array and read from the CSV, I wrote this
NSString *pathA = [NSString stringWithFormat:@"%@%@",
[[NSBundle mainBundle] resourcePath],
@"/right.wav"];
NSURL *filePathA = [NSURL fileURLWithPath:pathA isDirectory:NO];
AudioServicesCreateSystemSoundID((CFURLRef)filePathA, &rightSound);
NSMutableArray *newArray1 = [[NSMutableArray alloc]initWithCapacity:200];
NSMutableArray *newArray2 = [[NSMutableArray alloc]initWithCapacity:200];
NSMutableArray *newArray3 = [[NSMutableArray alloc]initWithCapacity:200];
NSMutableArray *newArray4 = [[NSMutableArray alloc]initWithCapacity:200];
NSMutableArray *newArray5 = [[NSMutableArray alloc]initWithCapacity:200];
self.questionArray = newArray1;
self.correctArray = newArray2;
self.wrongArray1 = newArray3;
self.wrongArray2 = newArray4;
self.wrongArray3 = newArray5;
[newArray1 release];
[newArray2 release];
[newArray3 release];
[newArray4 release];
[newArray5 release];
NSString* pathCSV = [[NSBundle mainBundle] pathForResource:@"capitals1" ofType:@"csv"];
NSString *fileString = [NSString stringWithContentsOfFile:pathCSV encoding:NSUTF8StringEncoding error:NULL];
if (!fileString) {
NSLog(@"Error reading file.");
}
NSScanner *scanner = [NSScanner scannerWithString:fileString];
[scanner setCharactersToBeSkipped:[NSCharacterSet characterSetWithCharactersInString:@"\n,;"]];
NSString *id = nil, *hashOne = nil, *hashTwo = nil, *hashThree = nil, *hashFour = nil, *hashFive=nil;
while ([scanner scanUpToString:@"," intoString:&id] && [scanner scanUpToString:@"," intoString:&hashOne] && [scanner scanUpToString:@"," intoString:&hashTwo] && [scanner scanUpToString:@"," intoString:&hashThree] && [scanner scanUpToString:@"," intoString:&hashFour] && [scanner scanUpToString:@"\n" intoString:&hashFive])
{
// NSLog(@"id:%@, 1:%@, 2:%@, 3:%@:, 4:%@, 5:%@", id, hashOne, hashTwo, hashThree, hashFour, hashFive);
[questionArray addObject:hashOne];
[correctArray addObject:hashTwo];
[wrongArray1 addObject:hashThree];
[wrongArray2 addObject:hashFour];
[wrongArray3 addObject:hashFive];
questionTotal++;
}
appDelegate.saveArrayQ = questionArray;
appDelegate.saveArrayC = correctArray;
appDelegate.saveArray1 = wrongArray1;
appDelegate.saveArray2 = wrongArray2;
appDelegate.saveArray3 = wrongArray3;
And then to access the array and display it as part of the quiz, I wrote this (the first NSString*question ...
line is where the SIGABRT error occurred).
showQuestion{
for (UIButton *thisButton in [self.view subviews])
{
if ([thisButton isKindOfClass:[UIButton class]])
{
if (thisButton.tag != 100)
{
[thisButton addTarget:self action:@selector(selectAnswer:) forControlEvents:UIControlEventTouchUpInside];
}else{}
}else{}
}
[answerButton1 setImage:[UIImage imageNamed:@"blue.png"] forState:UIControlStateNormal];
[answerButton2 setImage:[UIImage imageNamed:@"blue.png"] forState:UIControlStateNormal];
[answerButton3 setImage:[UIImage imageNamed:@"blue.png"] forState:UIControlStateNormal];
[answerButton4 setImage:[UIImage imageNamed:@"blue.png"] forState:UIControlStateNormal];
secondsLeft = appDelegate.timerLength;
NSString *timerString = [[NSString alloc]initWithFormat:@"0:%02d",secondsLeft];
timerLabel.text = timerString;
[timerString release];
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateDuration) userInfo:nil repeats:YES];
int q = arc4random() % 100;
q = abs(q);
NSString *num = [[NSString alloc]initWithFormat:@"%d",appDelegate.saveQ100 + 1];
numberLabel.text = num;
[num release];
NSString *question = [[NSString alloc]initWithFormat:@"%@",[appDelegate.saveArrayQ objectAtIndex:q]];
NSString *choice1 = [[NSString alloc]initWithFormat:@"%@",[appDelegate.saveArrayC objectAtIndex:q]];
NSString *choice2 = [[NSString alloc]initWithFormat:@"%@",[appDelegate.saveArray1 objectAtIndex:q]];
NSString *choice3 = [[NSString alloc]initWithFormat:@"%@",[appDelegate.saveArray2 objectAtIndex:q]];
NSString *choice4 = [[NSString alloc]initWithFormat:@"%@",[appDelegate.saveArray3 objectAtIndex:q]];
etc etc etc
Would really be grateful if someone can help me out here. I have spent 2 days trying to figure out what is wrong.
Upvotes: 0
Views: 460
Reputation:
When you generate a number which is stored in q , it will be a number from 0 to 99 . But from error message you have an array that have only 96 elements. You can not access an element that is not in array. I do not really know what you want to do , but just try to generate numbers from 0 to 95
int q = arc4random() % 96;
Upvotes: 1