C# 取消引用日期时间?上课时间
我不知道为什么会在这个问题上出现空引用异常C# 取消引用日期时间?上课时间,c#,linq,C#,Linq,我不知道为什么会在这个问题上出现空引用异常 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; // // Null
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
}
开始日期字段在此包装类中:
public class checksWrapper
{
public DateTime startDate { get; set; }
...
}
因为它是一个值类型,我认为我不应该使用新的运算符
如果调试器说它有一个日期{8/1/2012 12:00:00 AM},那么为什么我会得到一个空引用 crctw或
unitChecks
为空。变量dt
正常。您只是看到了=
..的错误一面,crctw
或unitChecks
为空。变量dt
正常。您只是看到了=
..错误的一面,请尝试crctw.unitChecks.startDate=dt.Value代码>要么crctw
要么crctw.unitChecks
计算为空。强制转换是冗余的(DateTime?->DateTime->DateTime?->DateTime)并且容易混淆。@pst OP正在调试,它不是空的,问题一定是从空到默认日期时间的强制转换。@pst确实,这是可能的too@AndreCalil我认为这是在转移注意力。dt必须是来自上述强制转换和空检查的DateTime
(不null
)。Trycrctw.unitChecks.startDate=dt.Value代码>要么crctw
要么crctw.unitChecks
计算为空。强制转换是冗余的(DateTime?->DateTime->DateTime?->DateTime)并且容易混淆。@pst OP正在调试,它不是空的,问题一定是从空到默认日期时间的强制转换。@pst确实,这是可能的too@AndreCalil我认为这是在转移注意力。dt必须是来自上述强制转换和空检查的日期时间
(不可空
)。