私の人生では、Site.Master ページにあるラベルで Null 参照例外が発生する理由がわかりません。同じページに他に 2 つのラベルがありますが、これらはエラーをスローしていません。
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>
lblOS ラベルは、エラーをスローする新しいものです。
私の Site.Master.cs ファイルのコードは次のとおりです。
protected void Page_Load(object sender, EventArgs e)
{
//Load App Store Icon
string strUserAgent = Request.UserAgent.ToString().ToLower();
if (lblOS.Text != null)
{
if (strUserAgent.Contains("iphone") || strUserAgent.Contains("ipad")){
lblOS.Text = "<a href='https://itunes.apple.com/'><img src='Images/appStore.jpg' class='connectIcons' title='App Store' alt='App Store' /></a><br />";
}
else{
lblOS.Text = null;
}
}
//Load Footer Links
string url = Request.ServerVariables["URL"];
url = url.Remove(0, url.LastIndexOf("/") + 1);
if (url == "Default.aspx") {
lblFooter.Text = GetGlobalResourceObject("mobileResource", "FooterNav").ToString();
}
else {
lblFooter.Text = GetGlobalResourceObject("mobileResource", "FooterHome").ToString() + GetGlobalResourceObject("mobileResource", "FooterNav").ToString();
}
}
「if (iOSLabel != null)」という行がエラーです。
他の場所で提案されているように、ここに記載されているコードを試し、次を追加しました。
Label iOSLabel = (Label)Master.FindControl("lblOS");
それに応じて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)