对象引用未设置为发现的对象实例

本文关键字:对象 实例 发现 设置 对象引用 | 更新日期: 2023-09-27 18:18:07

我有直播网站。在登录到管理模块,我得到这个错误。然而我的网站在过去的几天里工作得很好。没有任何新的上传副本,我得到这个错误。"/"应用程序中的服务器错误。

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error: 

Line 73:                                          <div id="ctl00_pnlloginname" style=" float:right; width:auto; padding-left:2px; padding-top:3px; padding-right:5px; height:20px;">
Line 74: 
Line 75:          Welcome <span class="subtitle"><span id="ctl00_lblloginname12"><asp:Label ID="lbldisplayname" runat="server"></asp:Label></span></span>&nbsp;|&nbsp;Online Users Found :&nbsp;<span class="subtitle"><asp:Label ID="lblonlineusers" runat="server"><%= Application["OnlineUsers"].ToString()%></asp:Label></span>&nbsp;|&nbsp;You Have <asp:Label ID="lblnewunreadmail" runat="server" CssClass="subtitle"></asp:Label>&nbsp;unread email in&nbsp;<a href="Pop3Client.aspx" class="subtitle">Gmail Inbox</a>&nbsp;|&nbsp;<a href="Report.aspx" class="subtitle">Track Report</a></div></td>
Line 76:                                         <tr>
Line 77:                                             <td height="34" align="center" valign="top" class="menu_bg">
Source File: c:'hostingspaces'ominvest'ominvestment.co.in'wwwroot'Admin'MasterPage.master    Line: 75 
Stack Trace: 

[NullReferenceException: Object reference not set to an instance of an object.]
   ASP.admin_masterpage_master.__Renderlblonlineusers(HtmlTextWriter __w, Control parameterContainer) in c:'hostingspaces'ominvest'ominvestment.co.in'wwwroot'Admin'MasterPage.master:75
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +256
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19
   System.Web.UI.WebControls.Label.RenderContents(HtmlTextWriter writer) +8697071
   System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer) +32
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +134
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19
   System.Web.UI.HtmlControls.HtmlForm.RenderChildren(HtmlTextWriter writer) +163
   System.Web.UI.HtmlControls.HtmlContainerControl.Render(HtmlTextWriter writer) +32
   System.Web.UI.HtmlControls.HtmlForm.Render(HtmlTextWriter output) +51
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99
   System.Web.UI.HtmlControls.HtmlForm.RenderControl(HtmlTextWriter writer) +40
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +134
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19
   System.Web.UI.Control.Render(HtmlTextWriter writer) +10
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +134
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19
   System.Web.UI.Page.Render(HtmlTextWriter writer) +29
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1266
Version Information: Microsoft .NET Framework Version:2.0.50727.5466; ASP.NET Version:2.0.50727.5456

我不知道是什么问题

Global.asax

string _pagePath;
    void Application_Start(object sender, EventArgs e)
            {
                Server.ClearError();
                Application["OnlineUsers"] = 0;
                // Caching the tracker image in memory
                byte[] trackerImg = File.ReadAllBytes(Context.Request.MapPath(ConfigurationManager.AppSettings["SD_Tut_ImageFileLocation"]));
                Application[ConfigurationManager.AppSettings["SD_Tut_ImageFileKeyName"]] = trackerImg;
                // Creating a new request queue collection
                int queueCapacity = Convert.ToInt32(ConfigurationManager.AppSettings["SD_Tut_TrackerRequestsToCache"]);
                Queue<TrackerRequest> trackerReqQueue = new Queue<TrackerRequest>(queueCapacity);
                Application[ConfigurationManager.AppSettings["SD_Tut_TrackerCachedRequestsKeyName"]] = trackerReqQueue;
            }
    void Session_Start(object sender, EventArgs e)
            {
                Session["username"] = Server.HtmlEncode(User.Identity.Name);
                if (Session.IsNewSession && Session["username"] == null)
                {
                    _pagePath = Server.MapPath("Default.aspx");
                    Response.Redirect(_pagePath);
                }
                Application.Lock();
                Application["OnlineUsers"] = (int)Application["OnlineUsers"] + 1;
                Application.UnLock();
            }
            void Session_End(object sender, EventArgs e)
            {
                Application.Lock();
                Application["OnlineUsers"] = (int)Application["OnlineUsers"] - 1;
                Session.Remove("username");
                Session.Clear();
                Application.UnLock();
            }

对象引用未设置为发现的对象实例

试着改变这个:

Application["OnlineUsers"].ToString()

:

(Application["OnlineUsers"] ?? 0).ToString()

您正在尝试转换字符串中的空值,这是不可能的,最终将导致此异常。

Application["OnlineUsers"]

可能为null,当您在第75行从该表达式调用.ToString()时,将抛出异常。最好提前检查null