2

I'm getting the error like below plz help me Advance Thanks, This is the error iam getting index 0 beyond bounds for empty array' *** First throw call stack:--->> Below is my code:

-(void)retriveContactsFromAddressBook
{
    //CFErrorRef error = NULL;
     ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);

    if (addressBook!=nil)
    {
        NSArray *allContacts=(__bridge_transfer NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);
        NSUInteger i=0;
        for ( i = 0; i<[allContacts count]; i++)
        {
            ABRecordRef contactPerson = (__bridge ABRecordRef)allContacts[i];
             NSString *firstName = (__bridge_transfer NSString*)ABRecordCopyValue(contactPerson, kABPersonFirstNameProperty);
            NSString *lastName=(__bridge_transfer NSString*)ABRecordCopyValue(contactPerson, kABPersonLastNameProperty);

            if (i==0) {
                self.FirstName=[NSMutableArray arrayWithObject:firstName];
                if (lastName==nil) {
                    self.LastName=[[NSMutableArray alloc]init];
                } else {
                    self.LastName=[NSMutableArray arrayWithObject:lastName];
                }

            } else {
                [self.FirstName addObject:firstName];

                if (lastName==nil) {
                    [self.LastName addObject:@""];
                } else {
                     [self.LastName addObject:lastName];
                }
            }
            NSString *full=[NSString stringWithFormat:@"%@ %@",[self.FirstName objectAtIndex:i],[self.LastName objectAtIndex:i]];

            if (i==0) {
                self.fullName=[NSMutableArray arrayWithObject:full];
            } else {
                [self.fullName addObject:full];
            }
        }
    }
    else
    {
        NSLog(@"Error Address book empty");
    }
}

I'm getting error like below---->>>

>Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
    *** First throw call stack:
    (0x2ce265f7 0x3a560c77 0x2cd3a157 0x126419 0x125edb 0x125cb7 0x302ce46d 0x30378325 0x3037824d 0x303777d1 0x3037750b 0x30377271 0x30377209 0x302cb97f 0x2fcf6f65 0x2fcf2951 0x2fcf27d9 0x2fcf21c7 0x2fcf1fd1 0x2fcebedd 0x2cded015 0x2cdea6f9 0x2cdeaafb 0x2cd37b31 0x2cd37943 0x340f0051 0x3032d6f1 0x1e1ea9 0x3aafcaaf)
    libc++abi.dylib: terminating with uncaught exception of type NSException
    (lldb) 
9
  • 2
    In the case where i=0 and last name is nil you allocate a new NSMutableArray but don't put anything in it. You then try and access the first element to build the full name - you should add an empty string as you do for the case where i>0 Commented Jan 20, 2015 at 11:56
  • See [self.FirstName objectAtIndex:i] & [self.LastName objectAtIndex:i] lines. Have you checked that both contains some values? Commented Jan 20, 2015 at 11:57
  • can u tell me clearly plzzz..@paulw Commented Jan 20, 2015 at 11:58
  • hear [self.FirstName objectAtIndex:i] the i value is showing "0" @Kampai Commented Jan 20, 2015 at 11:59
  • What about self.LastName array? Does it contains any value? Commented Jan 20, 2015 at 12:02

3 Answers 3

1
-(void)retriveContactsFromAddressBook
{
    //CFErrorRef error = NULL;
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);

    self.LastName=[[NSMutableArray alloc]init];
    self.FirstName=[[NSMutableArray alloc]init];
    self.fullName=[NSMutableArray arrayWithObject:full];

    if (addressBook!=nil)
    {
        NSArray *allContacts=(__bridge_transfer NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);
        NSUInteger i=0;
        for ( i = 0; i<[allContacts count]; i++)
        {
            ABRecordRef contactPerson = (__bridge ABRecordRef)allContacts[i];
            NSString *firstName = (__bridge_transfer NSString*)ABRecordCopyValue(contactPerson, kABPersonFirstNameProperty);
            NSString *lastName=(__bridge_transfer NSString*)ABRecordCopyValue(contactPerson, kABPersonLastNameProperty);

            if (firstName == nil) {
                [self.FirstName addObject:@""];
            } else {
                [self.FirstName addObject:firstName];
            }

            if (lastName==nil) {
                [self.LastName addObject:@""];
            } else {
                [self.LastName addObject:lastName];
            }

            NSString *full=[NSString stringWithFormat:@"%@ %@",[self.FirstName objectAtIndex:i],[self.LastName objectAtIndex:i]];

            [self.fullName addObject:full];
        }
    }
    else
    {
        NSLog(@"Error Address book empty");
    }
}

Use like this..

self.LastName=[[NSMutableArray alloc]init];

This line is the problem.. If lastname is nil you didn't insert any object

NSString *full=[NSString stringWithFormat:@"%@ %@",[self.FirstName objectAtIndex:i],[self.LastName objectAtIndex:i]];

But in this line [self.LastName objectAtIndex:i] you try to retrieve object from the empty array that is problem...

Sign up to request clarification or add additional context in comments.

1 Comment

0

Nayeemuddin you are accessing an array with no contents in it, so the exception is occurring.

NSString *full=[NSString stringWithFormat:@"%@ %@",[self.FirstName objectAtIndex:i],[self.LastName objectAtIndex:i]];

here you are accessing object of an array which does not exists. Also you forgot to initialise second array ,

 self.LastName=[[NSMutableArray alloc]init];

Comments

0

You need to insert a null entry into the last name array when you allocate it.

for ( i = 0; i<[allContacts count]; i++)
{
            ABRecordRef contactPerson = (__bridge ABRecordRef)allContacts[i];
             NSString *firstName = (__bridge_transfer NSString*)ABRecordCopyValue(contactPerson, kABPersonFirstNameProperty);
            NSString *lastName=(__bridge_transfer NSString*)ABRecordCopyValue(contactPerson, kABPersonLastNameProperty);

            if (i==0) {
                self.FirstName=[NSMutableArray arrayWithObject:firstName];
                if (lastName==nil) {
                    self.LastName=[[NSMutableArray arrayWithObject:@""];  // <--- This
                } else {
                    self.LastName=[NSMutableArray arrayWithObject:lastName];
                }

            } else {
                [self.FirstName addObject:firstName];

                if (lastName==nil) {
                    [self.LastName addObject:@""];
                } else {
                     [self.LastName addObject:lastName];
                }
            }

7 Comments

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath]; tableViewCell.accessoryType=UITableViewCellAccessoryCheckmark; tableViewCell.accessoryView.hidden = YES; self.selectedUsers=[[NSMutableArray alloc]initWithObjects:self.phoneNumber[indexPath.row],nil]; NSLog(@"selected users:%@",self.phoneNumber);
iam getting selected user is nill ??
Then I guess you have never initialised self.phoneNumber
wher i have to initislise it help me
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath]; tableViewCell.accessoryType=UITableViewCellAccessoryCheckmark; tableViewCell.accessoryView.hidden = YES; self.phoneNumber=[NSMutableArray arrayWithObject:@" "]; self.selectedUsers=[[NSMutableArray alloc]initWithObjects:self.phoneNumber[indexPath.row],nil]; NSLog(@"selected users:%@",self.phoneNumber);
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.