错误提示:对象引用未设置为对象的实例

时间:2022-06-12 20:34:06
public static string GetCurrentEmployeeName()
{
    return db.Employees.SingleOrDefault(
        c => c.NTUserName == CurrentUsername()
    ).Last_First;
}

There's no NULL data.

没有NULL数据。

2 个解决方案

#1


The default of a reference type is null, so if there is no matching record the SingleOrDefault method returns null.

引用类型的默认值为null,因此如果没有匹配的记录,则SingleOrDefault方法返回null。

#2


Well, anything could be null in the above statement and you would get the above error.

好吧,在上面的语句中任何东西都可以为null,你会得到上面的错误。

db
Employees
c.NTUserName

You will have to debug through your code and find it out for yourself.

您必须通过代码进行调试并自行查找。

#1


The default of a reference type is null, so if there is no matching record the SingleOrDefault method returns null.

引用类型的默认值为null,因此如果没有匹配的记录,则SingleOrDefault方法返回null。

#2


Well, anything could be null in the above statement and you would get the above error.

好吧,在上面的语句中任何东西都可以为null,你会得到上面的错误。

db
Employees
c.NTUserName

You will have to debug through your code and find it out for yourself.

您必须通过代码进行调试并自行查找。