<p>I have a web site in asp.net that uses a master page. In this master page I have a multiview control with one view that has all the content for the content pages and one view that has some local content to the master page where I show error messages from all content pages.</p>

<p>In this error view I have a asp.net Label control that displays the error messages. Usually the label works fine, but in some few cases the label is null, which renders a NullReferenceException. Now I have handled this case by checking if the label is null before using it, but still my question is:</p>

<p><em>Why is this label null? What are the circumstances that can generate this?</em></p>

<p><strong>EDIT:</strong> In the master page I have a method called SetErrorText that takes a string and sets the label. I'm calling this method from the content pages' Page_Load method, and this generally works fine. In all but two cases (that I've discovered so far) the label is initialised, and nothing separates these two cases from all the ones that work.</p>

<p>Also, all other controls in the master page are initialised, such as the View-control that houses the label.</p>

<p>When the Page_Load of a content page rolls around, the master page should be populated.</p>

[{'answer_id': 45348, 'author': 'Glenn Slaven', 'author_id': 2975, 'author_profile': 'https://Stackoverflow.com/users/2975', 'pm_score': 1, 'selected': False, 'text': '<p>What method on the master page are you accessing the label from?  Depending on the stage of the page lifecycle, the label control may not have been loaded yet</p>\n'}, {'answer_id': 45350, 'author': 'Daren Thomas', 'author_id': 2260, 'author_profile': 'https://Stackoverflow.com/users/2260', 'pm_score': 0, 'selected': False, 'text': '<p>Could you be accessing it before it is created? Check the page lifecycle: <a href="http://msdn.microsoft.com/en-us/library/ms178472.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/ms178472.aspx</a></p>\n'}, {'answer_id': 45371, 'author': 'Cros', 'author_id': 1523, 'author_profile': 'https://Stackoverflow.com/users/1523', 'pm_score': 3, 'selected': True, 'text': '<p>It seems that the problem was one of sloppiness. Someone had forgotten to delete the auto-generated Content-controls that Visual Studio throws in on all content pages where the master page has a ContentPlaceHolder-control.</p>\n\n<p>If a content page has a Content-control, all controls that are placed in the ContentPlaceHolder-control on the master page will be null, it seems.</p>\n'}, {'answer_id': 32367414, 'author': 'Chris Halcrow', 'author_id': 1549918, 'author_profile': 'https://Stackoverflow.com/users/1549918', 'pm_score': 0, 'selected': False, 'text': '<p>I had a very similar error. In my case it was caused by .NET compiler wierdness related to the control designer file. Even if the designer file has the controls defined correctly, delete it, re-generate it and rebuild (make sure to rebuild,  don\'t just \'build\'). See the top answer here for how to do regenerate the designer file:</p>\n\n<p><a href="https://stackoverflow.com/questions/45325/how-do-you-force-visual-studio-to-regenerate-the-designer-files-for-aspx-ascx-f">How do you force Visual Studio to regenerate the .designer files for aspx/ascx files?</a></p>\n'}]
['https://Stackoverflow.com/questions/45339', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1523/']