Sign up ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

I am trying to combine a string date and time then convert that to an NSDate. My code is:

NSMutableArray *arrayOfDatesAsDates = [[NSMutableArray alloc] init];

NSDateFormatter *dateAndTimeFormatter = [[NSDateFormatter alloc] init];    
[dateAndTimeFormatter setLocale:enUSPOSIXLocale];                           
[dateAndTimeFormatter setDateFormat:@"dd-MM-yyyy HH:mm"];

NSLog(@"here");
//create an NSDate with todays date and the right prayer time
NSString *prayerDateString = [curDate stringByAppendingString: @" "];
prayerDateString = [prayerDateString stringByAppendingString: timeOfMagrib];
NSDate *prayerDateAndTime = [dateAndTimeFormatter dateFromString:prayerDateString]; //convert string back to date
NSLog(@"nsdate %@", prayerDateAndTime);
[arrayOfDatesAsDates addObject:prayerDateAndTime];

The output to the log of prayerDateAndTime is 2013-07-08 20:26:00 +0000 as expected and the error message is Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'.

It crashes at the [arrayOfDatesAsDates addObject:prayerDateAndTime]; line.

Why is this?

Many thanks

share|improve this question
1  
"Why is this?" -[__NSArrayM insertObject:atIndex:]: object cannot be nil' –  Hot Licks Jul 8 '13 at 20:43
    
Did you debug and check the value? Is it being called multiple times so you see some good results in the log? –  Wain Jul 8 '13 at 20:43
2  
dateFromString may return nil. –  Ramy Al Zuhouri Jul 8 '13 at 20:44
1  
@samiles That's very suspicious. Are you sure that you pasted the text after "nsdate ", not before it? NSLog shows the time of the logging, and it will be very close to what you expect to see printed for prayerDateAndTime, so it is very easy to confuse one for the other. –  dasblinkenlight Jul 8 '13 at 20:52
1  
Can you post an exact copy of the relavent lines in the log? –  George Mitchell Jul 8 '13 at 21:04

1 Answer 1

up vote 1 down vote accepted

It looks like [dateAndTimeFormatter dateFromString:@"2013-07-08 20:26:00 +0000"] is returning nil because your date string "2013-07-08 20:26:00 +0000" does not match your dateFormat: @"dd-MM-yyyy HH:mm" ... try running after replacing:

[dateAndTimeFormatter setDateFormat:@"dd-MM-yyyy HH:mm"]

with

[dateAndTimeFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss ZZZ"]
// you really want this to match:     2013-07-08 20:26:00 +0000
// yyyy: 2013, four digit year
// MM: two digit numerical month
// dd: day of month
// HH: 24 hour hour
// mm: two digit minute
// ss: two digit second, zero padded
// ZZZ: time zone, {Z,Z,Z} -> RFC 822 GMT format

Format strings for Date Formats given here: http://www.unicode.org/reports/tr35/tr35-25.html#Date_Field_Symbol_Table

Timezone string acquired from: http://stackoverflow.com/a/3299389/2022405

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.