xcode中的SIGABRT错误:NSMutableArray无法加载创建空引用的所有对象

时间:2011-12-28 08:32:36

标签: ios xcode cocoa nsmutablearray sigabrt

我正在尝试创建一个测验应用。我的代码应该加载问题&从CSV中回答,然后随机显示。在模拟器上,它至少可以运行到85号。之后它开始抛出SIGABRT错误:

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]'

以下是我使用过的代码:

questionTota l和questionCount100初始化为int,然后设置数组并从CSV中读取,我写了这个

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; 

然后访问数组并将其显示为测验的一部分,我写了这个(第一个NSString*question ...行是发生SIGABRT错误的地方)。

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]];

等 等等 等

如果有人可以帮助我,真的很感激。我花了两天的时间试图找出问题所在。

1 个答案:

答案 0 :(得分:1)

当您生成一个存储在q中的数字时,它将是一个0到99之间的数字。但是从错误消息中你有一个只有96个元素的数组。您无法访问不在数组中的元素。我真的不知道你想做什么,但只是尝试生成0到95之间的数字

int q = arc4random() % 96;