0

我仍在学习用 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 was unhandled by user code

有人可以看看并建议我这里有什么问题吗?

4

2 回答 2

0

您可能想检查 UserName 是否也不为空

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);
}
于 2013-11-05T16:37:17.967 回答
0

检查以确保对象的UserName成员Proxy.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);
}
于 2013-11-05T16:38:14.907 回答