得到异常“索引0超出空数组的边界”?

时间:2011-07-12 18:40:46

标签: iphone exception nsmutablearray uidatepicker

我通过日期选择器查看用户的开始日期和结束日期。当用户以速度滚动选择器视图时,它会在那里崩溃并给我这个例外。那么解决方案是什么?

[

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



}

但如果我慢慢选择日期,则不会崩溃

3 个答案:

答案 0 :(得分:0)

您有一个没有任何对象的数组,并且您正在尝试访问第一个对象。如果您向我们展示代码,我们可以更具体。

答案 1 :(得分:0)

在上面的功能中,您没有释放对象FriendBL。你是如何存放房产的?保留或分配或复制?

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

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

答案 2 :(得分:0)

由于您没有按线程调用该方法,因此您不必使用自动释放池。你能试试下面的代码。

-(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]);
    }
}

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