object reference not set to an instance object RRS feed

  • Question

  • User-1634604574 posted

    i get error on high lighted code 

    using Microsoft.Web.Administration;

    ServerManager serverManager = new ServerManager(); //get the site (e.g. default) Site site = serverManager.Sites.FirstOrDefault(s => s.Name == "ERP3"); //Protocol hhtp, https.... httpcontext.current.response.write(site.Bindings.ElementAt(0).EndPoint.Address); //Puerto httpcontext.current.response.write(site.Bindings.ElementAt(0).EndPoint.Port);

    i get error on that high light 

    object reference not set to an instance object

    Wednesday, July 8, 2020 6:18 AM

All replies

  • User753101303 posted


    It happens when you try to call use an object property or method for an object which is null. Use the debugger (or add code checks) to see which object in your expression is null starting maybe with site.

    Wednesday, July 8, 2020 6:49 AM
  • User-1634604574 posted

    my demo don't have any problem with c# console but when i run it on asp.net web service i get this exception

    Wednesday, July 8, 2020 7:38 AM
  • User753101303 posted

    If you can't debug live you can add code such as :

    if (site==null) throw new Exception("site is null");
    if (site.Binbings==null) throw new Exception("Bindings is null"); 

    to find out which object is causing the issue. EndPoint is perhaps null if not binding is explicitely defined. Ypu may want also to handle having 0 to n bindings for your site etc...

    Wednesday, July 8, 2020 8:38 AM
  • User-1634604574 posted

    i have exception at both of them 

    if (site.Bindings == null) throw new Exception("Bindings is null");

    if (site == null) throw new Exception("site is null");

    Wednesday, July 8, 2020 8:54 AM
  • User-1634604574 posted

    can you run that demo at the first post on your side on asp.net web service?

    Wednesday, July 8, 2020 9:03 AM
  • User1173933630 posted


    HttpContext.Current only exists in the context of the Web application, the client does not exist HttpContext,You need to make sure that httpcontext is on the server and not the client.

    Best regards,

    Peng Ding

    Friday, July 24, 2020 7:07 AM