0
votes
-(void)loadquestion {
 NSBundle *bundle = [NSBundle mainBundle];
 NSString *textfilePath = [bundle pathForResource:@"MathPSLE" ofType:@"txt"];
 NSString *fileContents = [NSString stringWithContentsOfFile:textfilePath encoding:NSUTF8StringEncoding error:nil];
 NSArray *quizArray = [[NSArray alloc] initWithArray:[fileContents componentsSeparatedByString:@"\n"]];
 theQuiz = quizArray;
}

* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[NSCFArray objectAtIndex:]: index (-4( or possibly larger)) beyond bounds (300)'


-(void)askquestion {
 QuestionNumber = QuestionNumber +1;

 NSInteger row = 0;

 if (QuestionNumber == 1) {
  row = QuestionNumber -1;
 }
 else {
  row = ((QuestionNumber -1 *6));
 }

 NSString *selected  = [theQuiz objectAtIndex:row];
 NSString *activeQuestion = [[NSString alloc] initWithFormat:@"Question: %@",selected];
 [ansButton1 setTitle:[theQuiz objectAtIndex:row+1] forState: UIControlStateNormal];
 [ansButton2 setTitle:[theQuiz objectAtIndex:row+2] forState: UIControlStateNormal];
 [ansButton3 setTitle:[theQuiz objectAtIndex:row+3] forState: UIControlStateNormal];
 [ansButton4 setTitle:[theQuiz objectAtIndex:row+4] forState: UIControlStateNormal];
 rightAnswer = [[theQuiz objectAtIndex:row+5]intValue];

 questionLabel.text = activeQuestion;

 [selected release];
 [activeQuestion release];
}

-(IBAction)next_Button {
 [self askquestion];
}

yes. when i insert the breakpoint. the error message is here.

1
Are you sure that the loadquestion method is where the error is from?dreamlax
I agree with dreamlax, I don't see it happening there.Matthew Frederick
yes. when i insert the breakpoint. the error message is here.lol
i have insert another set of code. the error is it there?lol

1 Answers

1
votes

It's pretty obvious from your other code segment the error is somewhere in one of the [theQuiz objectAtIndex:row+x] lines.

It seems like the number of questions you have in your file may not match the maximum number of questions your code thinks it has.

Another thing to look at would be row = ((QuestionNumber -1 *6)).. are you sure that's not winding up negative?