获取异常"index 0 beyond bounds for empty array"?



我通过日期pickerview从用户获得开始日期和结束日期。当用户快速滚动pickerview时,它会崩溃,然后给我这个异常。那么解决方案是什么呢?

(

NSMutableArray objectAtIndex:]: index 0 beyond bounds for empty array'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x013fabe9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x0154f5c2 objc_exception_throw + 47
    2   CoreFoundation                      0x013f06e5 -[__NSArrayM objectAtIndex:] + 261
    3   iFeel                               0x0001e1b7 -[S7GraphView drawRect:] + 5714
    4   UIKit                               0x0047d6eb -[UIView(CALayerDelegate) drawLayer:inContext:] + 426
    5   QuartzCore                          0x01f6f9e9 -[CALayer drawInContext:] + 143
    6   QuartzCore                          0x01f6f5ef _ZL16backing_callbackP9CGContextPv + 85
    7   QuartzCore                          0x01f6edea CABackingStoreUpdate + 2246
    8   QuartzCore                          0x01f6e134 -[CALayer _display] + 1085
    9   QuartzCore                          0x01f6dbe4 CALayerDisplayIfNeeded + 231
    10  QuartzCore                          0x01f6038b _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 325
    11  QuartzCore                          0x01f600d0 _ZN2CA11Transaction6commitEv + 292
    12  QuartzCore                          0x01f907d5 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 99
    13  CoreFoundation                      0x013dbfbb __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 27
    14  CoreFoundation                      0x013710e7 __CFRunLoopDoObservers + 295
    15  CoreFoundation                      0x01339bd7 __CFRunLoopRun + 1575
    16  CoreFoundation                      0x01339240 CFRunLoopRunSpecific + 208
    17  CoreFoundation                      0x01339161 CFRunLoopRunInMode + 97
    18  GraphicsServices                    0x019de268 GSEventRunModal + 217
    19  GraphicsServices                    0x019de32d GSEventRun + 115
    20  UIKit                               0x0045442e UIApplicationMain + 1160
    21  iFeel                               0x00002990 main + 102
    22  iFeel                               0x00002921 start + 53
)
terminate called after throwing an instance of 'NSException'

@deepak和@ Grady Player讨论的方法是:

-(void)getGraphicalData{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    //[NSThread sleepForTimeInterval:1];
    self.friendBL = [[FriendBL alloc] init];
    self.result = [self.friendBL  getGraphicalData1:txtStartTime.text andTodate:txtEndTime.text];
    if ([self.result isKindOfClass:[NSString class]]) {
        UIAlertView * friendListAlertView = [[UIAlertView alloc] initWithTitle:@"Friend List" message:(NSString*) self.result delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [friendListAlertView show];
        [friendListAlertView release];
    }
    else {
        self.reportList = (NSMutableArray*) self.result;
        NSLog(@"%d",[reportList count]);
    }
    [pool release];
}

在选择日期后,我点击按钮说完成这是代码

-(IBAction)doneBtn{
    [dateView setHidden:YES];
    NSLog(@"%@",[dateTimePicker date]);
    NSLocale *locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateStyle:NSDateFormatterBehaviorDefault];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"MM/dd/yyyy hh:mm:ss"];
    dateFormat.locale = locale;
    if (tag) {
        txtEndTime.text=[dateFormat stringFromDate:[dateTimePicker date]];
        if (![self compareDates]) {
            txtStartTime.text=@"";
            txtEndTime.text=@"";
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning!" message:@"Date is not in valid" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
            [alert show];
            [alert release];
        }
    }
    else {
        txtStartTime.text=[dateFormat stringFromDate:[dateTimePicker date]];
    }
    NSLog(@"%@",[dateFormat stringFromDate:[dateTimePicker date]]);

}

但是如果我慢慢选择日期它不会崩溃

由于您不是通过线程调用方法,因此您不必使用自动释放池。你可以试试下面的代码吗?

-(void)getGraphicalData{
    self.friendBL = [[[FriendBL alloc] init] autorelease];
    self.result = [self.friendBL  getGraphicalData1:txtStartTime.text andTodate:txtEndTime.text];
    if ([self.result isKindOfClass:[NSString class]]) {
        UIAlertView * friendListAlertView = [[UIAlertView alloc] initWithTitle:@"Friend List" message:(NSString*) self.result delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [friendListAlertView show];
        [friendListAlertView release];
    }else {
        self.reportList = (NSMutableArray*) self.result;
        NSLog(@"%d",[reportList count]);
    }
}

这是我最后一次尝试帮助:-(

您有一个没有任何对象的数组,并且您试图访问第一个对象。如果你给我们看代码,我们可以更具体。

在上面的函数中,您没有释放对象FriendBL。你是如何存放你的财产的?保留、分配还是复制?

可能是自动释放池的问题。函数是否在主线程中调用?

如果你能给我们更多的见解,可能会有用。

最新更新