0

I am still learning to program in C# and working on a project in office

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);
}

This is where I get this exception: NullReferenceException was unhandled by user code

Can someone please take a look and suggest me what could be be wrong here?

ataravati
  • 8,891
  • 9
  • 57
  • 89
vishal
  • 640
  • 1
  • 5
  • 21
  • 5
    `UserName` is probably null. – Robert Harvey Nov 05 '13 at 16:33
  • 3
    And what did your debugger tell you? I can never understand why the majority of `NullReference` type exceptions are ever posted to SO when it is *faster* to run your debugger which will tell you *what* is null. – Moo-Juice Nov 05 '13 at 16:33
  • 2
    A `NullReferenceException` cries for the debugger. _"I am still learning to program"_ Then now is the best time to [learn how to use it](http://msdn.microsoft.com/en-us/library/sc65sadd(v=vs.90).aspx). – Tim Schmelter Nov 05 '13 at 16:34
  • Have got at least the line Nº? – Agustin Meriles Nov 05 '13 at 16:37
  • 1
    http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it ....21k views and still, one of the most common issues posted. – Arran Nov 05 '13 at 16:37
  • `Proxy.ClientCredentials` is static class ? – Elshan Nov 06 '13 at 08:02

2 Answers2

0

You may want to check whether UserName is also not null

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);
}
DobleA
  • 334
  • 1
  • 3
  • 13
0

Check to make sure that the UserName member of the Proxy.ClientCredentials object is not null as well. Something like this:

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);
}
joe_coolish
  • 7,201
  • 13
  • 64
  • 111
  • ok trying,, I just started with C# last month and this is my forst program ,, just bear with me guys ... – vishal Nov 05 '13 at 17:04
  • got it,,, debug step by step and found environment I was hitting wasnt configured correctly to accept username and password... – vishal Nov 05 '13 at 17:15