NullReferenceException is a runtime exception in C# that occurs when you try to access members of an empty object. This can happen when:
There are several ways to solve NullReferenceException:
if (object != null)
{
// 访问对象的成员。
}
string name = object?.Name ?? "Unknown";
string name = object?.Name;
try
{
// 访问对象的成员。
}
catch (NullReferenceException ex)
{
Console.WriteLine("NullReferenceException 发生。");
}
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3