Ali
Ali

Reputation: 11590

UIAddressBook code puts error on device

I testing following code on simulator ,it works fine but when I select the device and run then it gives exception .

ABAddressBookRef addressBook = ABAddressBookCreate();

CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);  
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);

tempPeoples= [NSMutableArray arrayWithCapacity:0];


for(int i=0;i<nPeople;i++){

    ABRecordRef i1=CFArrayGetValueAtIndex(allPeople, i);

    NSString* name = (NSString *)ABRecordCopyValue(i1,kABPersonFirstNameProperty);

    [tempPeoples addObject:name];

//  [peoples addObject:i1];

}// end of

following exception occurs

2011-01-06 12:12:42.384 Appointment[2849:207] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSCFArray insertObject:atIndex:]: attempt to insert nil' 2011-01-06 12:12:42.397 Appointment[2849:207] Stack: ( 843263261, 825818644, 842812211, 842812115

Please help

Upvotes: 0

Views: 162

Answers (1)

Abizern
Abizern

Reputation: 150665

You're adding a nil to an array (as the message says)

By a process of deduction, I see that you are adding objects to an array at this line

[tempPeoples addObject:name];

So it is likely that, for this snippet of code, this is where the error happens.

Probably, Not all of the Contacts have a first name, which is likely to be the case for contacts that are businesses rather than people.

You could put a breakpoint in the code and run it through a debugger to see what conditions cause this.

Upvotes: 1

Related Questions