自己用C#写了个系统,但访问网站动态页面(除首页静态生成外),经常会出现:
Object reference not set to an instance of an object.这样的错误,但重新刷新一下又能正常访问,查了代码也能正常运行,请大虾帮忙看下,谢谢,以下是一次出现的全部提示:Server Error in '/' Application.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.]
   ArticlesBean.GetRecordNum(Int32 id, Int32 classType) +105
   ysys_index.Page_Load(Object sender, EventArgs e) +175
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
   System.Web.UI.Control.OnLoad(EventArgs e) +91
   System.Web.UI.Control.LoadRecursive() +74
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1022重新刷新一下又会正常显示。网站是:http://www.18ys.cn 谢谢。

解决方案 »

  1.   

    这种问题是程序必然的流程的结果,没有什么“一抄就灵”的答案。如果你是老板,那么找别人去编程。如果你是程序员,而你没有心情去使用vs进行调试,你只是一遍遍“刷新”或者其它什么乱点一通,那么建议你找一个会编程的人坐在你旁边每隔5秒钟就告诉你该按哪个键,该干什么。这就行了。不会调试,不知道让vs自动捕获异常并且把调试光标直接停在抛出异常的语句上,不知道调试变量和调用堆栈,这就好像对着一个不会系鞋带的小学三年级新生,只能让他先学习系鞋带。
      

  2.   

    ArticlesBean.GetRecordNum(Int32 id, Int32 classType)
    贴出这个方法的源代码
      

  3.   

    我只是见过一些搞手工调试的人,用鼠标“点点点”之后扔出一个表面的bug来让程序员给“修复”。可那是因为人家程序员有源代码,并且也有功夫去重现你的问题,而且手工测试人员提交的问题报告确实是可重现的。公司里条件那么充分的情况下,提交bug问题是有规格要求的。论坛上更是如此,在论坛上发帖子要有必要的内容。
      

  4.   

        public static int GetRecordNum(int id, int classType)
        {
            int recordNum = 0;
            string sql = "select count(A.articleId) as num from articles as A,articleclass as B where A.isPassed=1 and A.fatherId=B.classId";
            if (classType == 0)
            {
                sql = sql + " and A.fatherId=" + id;
            }
            else
            {
                sql = sql + " and A.classId=" + id;
            }        DataBase db = new DataBase();
            SqlDataReader sdr = null;
            
            sdr = db.getReader(sql);
            sdr.Read();
            recordNum = Convert.ToInt32(sdr["num"]);
            sdr.Close();
            return recordNum;
        }程序在本地测试都能正常运行,vs调试是也是正常运行的,没错误提示。发布到网上,只是偶尔不同的页面会出现Object reference not set to an instance of an object.这种错误,但刷新下页面又能正常访问,并且没有任何错误,这种错误也是不定时的出现。
      

  5.   


    做try{}catch(){}捕获一下异常!!