C# 用户代码C未处理NullReference异常#

C# 用户代码C未处理NullReference异常#,c#,nullreferenceexception,C#,Nullreferenceexception,我还在学习C语言编程,还在办公室里做一个项目 public static void SetIdentity(string subId) { if (Proxy.ClientCredentials != null) { Proxy.ClientCredentials.UserName.UserName = subId;// Proxy.ClientCredentials.UserName.Password = subId; } S

我还在学习C语言编程,还在办公室里做一个项目

public static void SetIdentity(string subId)
{
    if (Proxy.ClientCredentials != null)
    {
        Proxy.ClientCredentials.UserName.UserName = subId;// 
        Proxy.ClientCredentials.UserName.Password = subId;
    }

    ServicePointManager.ServerCertificateValidationCallback += new System.Net.Security.RemoteCertificateValidationCallback(customXertificateation);
}
这就是我得到此异常的原因:
NullReferenceException未被用户代码处理


有人能看一下并告诉我这里有什么问题吗?

您可能需要检查用户名是否也不为空

public static void SetIdentity(string subId)
{
    if (null != Proxy.ClientCredentials && null != Proxy.ClientCredentials.Username)
    {
        Proxy.ClientCredentials.UserName.UserName = subId;// 
        Proxy.ClientCredentials.UserName.Password = subId;
    }

    ServicePointManager.ServerCertificateValidationCallback += new System.Net.Security.RemoteCertificateValidationCallback(customXertificateation);
}

检查以确保
代理.ClientCredentials
对象的
用户名
成员也不为空。大概是这样的:

public static void SetIdentity(string subId)
{
    if (Proxy.ClientCredentials != null && Proxy.ClientCredentials.UserName != null)
    {
        Proxy.ClientCredentials.UserName.UserName = subId;// 
        Proxy.ClientCredentials.UserName.Password = subId;
    }

    ServicePointManager.ServerCertificateValidationCallback += new System.Net.Security.RemoteCertificateValidationCallback(customXertificateation);
}

UserName
可能为空。调试器告诉了您什么?我永远无法理解为什么大多数
NullReference
类型异常都会被发布到这样的位置,因为运行调试器会更快地告诉您什么是null。一个
NullReferenceException
需要调试器。“我仍在学习编程”那么现在是最好的时间了。至少有一行Nº。。。。21k视图,但仍然是最常见的问题之一。很有趣,因为整个代码块中几乎所有内容都可能为空…我们甚至不知道代理是什么。Yoda条件非常简单。好吧,尝试一下,我上个月刚开始使用C,这是我的forst程序,请大家耐心等待,明白了吗,,,一步一步地调试,发现我遇到的环境没有正确配置为接受用户名和密码。。。