Asp.net Site.Master中标签上出现空引用异常

Asp.net Site.Master中标签上出现空引用异常,asp.net,c#-4.0,Asp.net,C# 4.0,就我的一生而言,我无法理解为什么我在我的Site.Master页面中的标签上出现空引用异常。我在同一页上有另外两个标签,它们没有抛出错误 以下是包含所有3个标签的DIV: <div class="footer"> <asp:Label ID="lblOS" runat="server" /> <asp:Label ID="lblFooter" runat="server" /><br /> <asp:Label ID=

就我的一生而言,我无法理解为什么我在我的Site.Master页面中的标签上出现空引用异常。我在同一页上有另外两个标签,它们没有抛出错误

以下是包含所有3个标签的DIV:

<div class="footer">
    <asp:Label ID="lblOS" runat="server" />
    <asp:Label ID="lblFooter" runat="server" /><br />
    <asp:Label ID="lblFooterEx" runat="server" Text="<% $Resources:mobileResource,FooterExtra %>" />
</div>
并相应地修改了CS代码

当我测试它时,新的iOSLabel现在抛出null异常

关于如何解决这个问题有什么想法吗

编辑: 这是生成的错误

Error: System.Web.HttpUnhandledException (0x80004005): Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.NullReferenceException: Object reference not set to an instance of an object.
   at CLVmobileApp.SiteMaster.Page_Load(Object sender, EventArgs e) in C:\Visual Studio\AS-MobileCLV\CLVmobileApp\Site.Master.cs:line 15
   at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
   at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
   at System.Web.UI.Control.OnLoad(EventArgs e)
   at System.Web.UI.Control.LoadRecursive()
   at System.Web.UI.Control.LoadRecursive()
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
   at System.Web.UI.Page.HandleError(Exception e)
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
   at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
   at System.Web.UI.Page.ProcessRequest()
   at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context)
   at System.Web.UI.Page.ProcessRequest(HttpContext context)
   at ASP.default_aspx.ProcessRequest(HttpContext context) in c:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\cbd2add4\2961a9ef\App_Web_ddrybdwm.17.cs:line 0
   at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
而不是

Label iOSLabel = (Label)Master.FindControl("lblOS");
试一试


您在哪里访问该标签(即母版页本身或内容页上)?带有我标签的DIV仅在我的网站母版页上。(这是我网站的页脚。)我在示例代码中没有看到有错误的那一行。我不确定你指的是哪一行,但我在我的页面加载中的“if(lblOS.Text!=null)”行上看到了错误。我尝试添加“Label iOSLabel=(Label)Master.FindControl(“lblOS”);”并将所有“lblOS”更改为“iOSLabel”,然后空引用异常出现在“Label iOSLabel”行上。无论我第一次在哪里引用那个标签,我都会得到错误。这个错误是在每次加载页面时发生的,还是只是偶尔发生的?另外,你能复制并粘贴错误信息的全文吗?这似乎有点可疑。
Error: System.Web.HttpUnhandledException (0x80004005): Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.NullReferenceException: Object reference not set to an instance of an object.
   at CLVmobileApp.SiteMaster.Page_Load(Object sender, EventArgs e) in C:\Visual Studio\AS-MobileCLV\CLVmobileApp\Site.Master.cs:line 15
   at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
   at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
   at System.Web.UI.Control.OnLoad(EventArgs e)
   at System.Web.UI.Control.LoadRecursive()
   at System.Web.UI.Control.LoadRecursive()
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
   at System.Web.UI.Page.HandleError(Exception e)
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
   at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
   at System.Web.UI.Page.ProcessRequest()
   at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context)
   at System.Web.UI.Page.ProcessRequest(HttpContext context)
   at ASP.default_aspx.ProcessRequest(HttpContext context) in c:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\cbd2add4\2961a9ef\App_Web_ddrybdwm.17.cs:line 0
   at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
Label iOSLabel = (Label)Master.FindControl("lblOS");
Label iOSLabel = (Label)(Master as MyMasterPage) .FindControl("lblOS");