locked
this.Master returns “object reference not set to an instance of an object” RRS feed

  • Question

  • User-1616316942 posted

    Hi everyone, I have an asp.net page that gets loaded inside a customer's website. It loads fine the first time, but when the customer clicks a button, it crashes with the "object reference not set to an instance of an object" error. The log shows that the error occurred in the this.Master line (see below):

    protected override void OnInit(EventArgs e)
    {
        _master = (CustomMasterPage)this.Master; //this is the line where exception occurs
        //...some other code
    }

    What I know so far:

    • I can't reproduce the error in neither DEV nor QA environments.
    • I can't reproduce the problem when the inner page is executed on its own, outside of the client's page.
    • Our prod servers are load-balanced, but it shouldn't be a session issue, right? Even if I was suddenly served a brand new instance of Session, "this" still would not be null as it gets created by Asp.Net as part of request/response. All the custom values would disappear, yes, but "this" should still exist, right?

    So, I'm looking for ideas explaining why 'this' would suddenly become null in the described scenario, thanks!

    Wednesday, February 5, 2020 8:22 PM

All replies

  • User475983607 posted

    Hi everyone, I have an asp.net page that gets loaded inside a customer's website.

    What does "loaded inside a customer's web site" mean?  Loaded in an iFrame?

    Wednesday, February 5, 2020 8:28 PM
  • User-1616316942 posted

    What does "loaded inside a customer's web site" mean?  Loaded in an iFrame?

    Yes, they load our page in an iframe

    Wednesday, February 5, 2020 8:33 PM
  • User475983607 posted

    According to the docs...

    The OnInit method performs the initialization and setup steps required to create a Page instance. In this stage of the page's life cycle, declared server controls on the page are initialized to their default state; however, the view state of each control is not yet populated. A control on the page cannot access other server controls on the page during the Page_Init phase, regardless of whether the other controls are child or parent controls. Other server controls are not guaranteed to be created and ready for access.

    I recommend using a different event initialization or page life cycle event.

    Wednesday, February 5, 2020 9:20 PM