我的程序在本机运行正常,但发布到服务器时,经常出现如下错误,刷新一下就好了,但进入网页后台一点错误都没有,前台出现错误时偶然的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: An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  Stack Trace: 
[NullReferenceException: Object reference not set to an instance of an object.]
   Controls_ContactBar.Page_Load(Object sender, EventArgs e) in e:\lq\ASPDOTNET05\ECSystem\WebUI\Controls\ContactBar.ascx.cs:19
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +15
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +34
   System.Web.UI.Control.OnLoad(EventArgs e) +99
   System.Web.UI.Control.LoadRecursive() +47
   System.Web.UI.Control.LoadRecursive() +131
   System.Web.UI.Control.LoadRecursive() +131
   System.Web.UI.Control.LoadRecursive() +131
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1061 
--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET Version:2.0.50727.42 
前台和后台相比多了 cache,母板,和用户控件要是这些导致出错,本机测试也应该出现的,本机测试一点问题都没郁闷阿,各位高手帮忙了

解决方案 »

  1.   

    Controls_ContactBar.Page_Load(Object sender, EventArgs e) in e:\lq\ASPDOTNET05\ECSystem\WebUI\Controls\ContactBar.ascx.cs:19写得很明白啊
      

  2.   

    检查代码
    e:\lq\ASPDOTNET05\ECSystem\WebUI\Controls\ContactBar.ascx.cs:19
      

  3.   

    用异常 捕获
    e:\lq\ASPDOTNET05\ECSystem\WebUI\Controls\ContactBar.ascx.cs:19这里的错误.
      

  4.   

    19行是 读cache本机运行没事的我的cacahe是在global的Application_BeginRequest里初始化有错么
      

  5.   

    19行代码:        int ID = (int)HttpContext.Current.Cache["ID"];
      

  6.   

    System.NullReferenceException: Object reference not set to an instance of an object.
    int ID = (int)HttpContext.Current.Cache["ID"];
    HttpContext.Current.Cache["ID"]为null时就会引发,加个if过滤一下
      

  7.   

    同意楼上  另外 本机测试没问题的话 建议你检查
    1 web.config
    2 数据内容
      

  8.   

    。。要是因为cache为空 本机也会出错的阿
      

  9.   

    (1)本机环境是不是IIS,IIS版本是否一致,另外,在本机是否与生产环境一下长时间运行。
    (2)查找一下关于IIS的进程回收,垃圾回收等内容。
    (3)不用cache,改用静态类存贮,看看是否还会出现问题。总体上,怀疑cache被IIS回收或其它原因造成进程IIS重启后cache被清空。
      

  10.   

    我在 Application_BeginRequest里有:
    if(HttpContext.Current.Cache["ID"]==null)
      {
        初始化cache
    }即使在页面里cache为空,请求时也会被初始化啊Application_BeginRequest 不是执行每次请求前先执行的么?
      

  11.   

    HttpContext.Current.Cache["ID"]为null你在19行之前有用if判断一下它是否为null吗?不读取就尝试用(int)来强类型转换就会造成潜在的危险,因为Cache和Session这类东西随时可能为null。因此,以后凡是类似的情景,你都必须先判断,用if或者用?:。
      

  12.   

    我在 Application_BeginRequest里有:
    if(HttpContext.Current.Cache["ID"]==null)
      {
        初始化cache
    }即使在页面里cache为空,请求时也会被初始化啊Application_BeginRequest 不是执行每次请求前先执行的么?(1)加测试语句,看是否的确在请求之间执行,并输出cache值。特别是出错的时候要注意。
    (2)如果以上没有问题,那么错误不在这个语句上。