获取错误:字符串引用未设置为String的实例。 参数名称:s

我正在使用此代码将日期时间从我的数据库截断为其年份和时间组件。 变量YearOfRelease和Runtime包含格式为“dd / MM / yyyy hh:mm:ss”的日期时间以前工作正常,但它现在给出错误:

字符串引用未设置为String的实例。 参数名称:s

它只能是DateTime.ParseExact函数中的错误,任何人都可以让我知道为什么’null’突然导致这个问题,因为以前它工作得很好?

DateTime dt2 = new DateTime(); dt = DateTime.ParseExact(YearOfRelease, "dd/MM/yyyy hh:mm:ss", null); Year = dt.Year.ToString(); dt2 = DateTime.ParseExact(RunTime, "dd/MM/yyyy hh:mm:ss", null); string hour = dt2.Hour.ToString(); string min = dt2.Minute.ToString(); Time = hour + ":" + min; 

DateTime.ParseExact的第一个参数是一个名为’s’的字符串参数。

因此,在程序中看起来像YearOfRelease或RunTime为null。 确保在调用DateTime.ParseExact之前设置它们。