desreferenciar DateTime? a clase.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?

preguntado el 24 de agosto de 12 a las 03:08

Trata crctw.unitChecks.startDate = dt.Value; -

Ambos crctw or crctw.unitChecks evaluate to null. The casts are redundant (DateTime? -> DateTime -> DateTime? -> DateTime) and confusing. -

@pst OP is debugging and it's not null, the problem must be the cast from nullable to default datetime. -

@pst Indeed, that's possible too -

@AndreCalil I believe it's a red-herring. The dt debe: ser una DateTime (¿No estás registrado como Nullable<DateTime>) from above cast and null-check. -

1 Respuestas

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

Respondido 24 ago 12, 03:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.