0

I have a segmentedControl button and a table view below the segmentedControl.

In my viewDidLoad method, im fetching the values from core data and populating the array as follows,

 NSError *Error = nil;
    APIRequest *apiRequest = [[APIRequest alloc]init];
    [apiRequest showPendingData];    
    NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"PendingShipmentDetails"];
    self.devices = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"PendingShipmentDetails" inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];
    NSArray *fetchedObjects = [managedObjectContext executeFetchRequest:fetchRequest error:&Error];
    shipmentReferenceNumberArray = [[NSMutableArray alloc]init];

    for (NSManagedObjectContext * info in fetchedObjects)

    {        
        [shipmentReferenceNumberArray addObject:[info valueForKey:@"shipmentno"]];

    }

In handleSelection method, Im handling button clicks as follow

- (void)handleSelection:(id)sender
{
    UISegmentedControl *segmentControl = (UISegmentedControl *) sender;
    NSInteger selectedSegment = segmentControl.selectedSegmentIndex;

    if (selectedSegment == 0)
    {
        //toggle the correct view to be visible
        [self.myOrdersTableView setHidden:NO];
        [self.completedOrdersTableView setHidden:YES];
        NSLog(@"pending");
    }
    else
    {
        //toggle the correct view to be visible
        [self.completedOrdersTableView setHidden:NO];
        [self.myOrdersTableView setHidden:YES];
        [self.devices removeAllObjects];
        NSError *Error = nil;
        APIRequest *apiRequest = [[APIRequest alloc]init];
        [apiRequest showCompletedData];

        NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
        NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"CompletedShipmentDetails"];
        self.devices = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"CompletedShipmentDetails" inManagedObjectContext:managedObjectContext];
        [fetchRequest setEntity:entity];
        NSArray *fetchedObjects = [managedObjectContext executeFetchRequest:fetchRequest error:&Error];


    }
}

Now if I press the segmented control button whose index is 1 Im getting the following error

  • Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]' * First throw call stack: (0x2867d5f7 0x36347c77 0x28591157 0x15bbd3 0x2bde795d 0x2bde7a1f 0x2bddd141 0x2bbf51cb 0x2bb1f19f 0x2b54af65 0x2b546951 0x2b5467d9 0x2b5461c7 0x2b545fd1 0x2b5989f1 0x2fe5f82f 0x295a71ed 0x286333a5 0x286437f3 0x2864378f 0x28641db1 0x2858eb31 0x2858e943 0x2f952051 0x2bb80f21 0x164195 0x368e3aaf) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)

How can I sort this out?

  • 1
    your array might be nil, check array in log – Mr. Bond Apr 25 '16 at 06:46
  • 3
    Set an exception breakpoint to determine where you are trying to access the third element of a two element array. It doesn't look like it would be in the code you showed – Paulw11 Apr 25 '16 at 06:50
  • third element of a two element array means? –  Apr 25 '16 at 06:54
  • Set an exception breakpoint to array and check where application is crash..@user1241241 – Mr. Bond Apr 25 '16 at 06:55
  • As the exception message says, you have an array with two objects in it -0 & 1. You tried to access object 2. – Paulw11 Apr 25 '16 at 06:59
  • In cellForRowAtIndexPath im accessing it as NSManagedObject *device = [self.devices objectAtIndex:indexPath.row]; –  Apr 25 '16 at 07:07

0 Answers0