desreferenciar DateTime? a clase.DateTime
Frecuentes
Visto 99 veces
0
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?
1 Respuestas
3
Ambos crctw
or unitChecks
is null. The variable dt
is 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 c# linq or haz tu propia pregunta.
Trata
crctw.unitChecks.startDate = dt.Value;
- Andre CalilAmbos
crctw
orcrctw.unitChecks
evaluate to null. The casts are redundant (DateTime? -> DateTime -> DateTime? -> DateTime) and confusing. - user166390@pst OP is debugging and it's not null, the problem must be the cast from nullable to default datetime. - Andre Calil
@pst Indeed, that's possible too - Andre Calil
@AndreCalil I believe it's a red-herring. The dt debe: ser una
DateTime
(¿No estás registrado comoNullable<DateTime>
) from above cast and null-check. - user166390