micahhoover
micahhoover

Reputation: 2160

dereference DateTime? to class.DateTime

Some how I'm getting a null reference exception on this, and I'm not sure why.

if (units.Min(sd => sd.MONTH_UNIT_APPLIES_TO) != null)
{
    DateTime? dt = (DateTime)units.Min(sd => sd.MONTH_UNIT_APPLIES_TO);
    // dt = {8/1/2012 12:00:00 AM}
    crctw.unitChecks.startDate = (DateTime)dt;  // // NullReferenceException here
}

The start date field is in this wrapper class:

public class checksWrapper
{
    public DateTime startDate { get; set; }
    ...
}

Since it's a value type, I don't think I should have to use the new operator.

If the debugger says it has a date {8/1/2012 12:00:00 AM} then why am I getting a null reference?

Upvotes: 0

Views: 124

Answers (1)

code4life
code4life

Reputation: 15794

Either crctw or unitChecks is null. The variable dtis fine. You're just looking at the wrong side of the =...

Upvotes: 3

Related Questions