DataRow newone = allTable.NewRow();newone[0] = ++id;
newone[1] = name;
newone[2] = DateTime.Now.AddSeconds(-60);
newone[3] = DateTime.Now;
newone[4] = "";
newone[5] = "";
newone[6] = false;
newone[7] = false;
newone[8] = "";allTable.Rows.Add(newone);程序比较大,我通过不断尝试,发现如果程序不加上面的代码运行很久都没有问题,
但是加了上面的代码就出现NullReferenceException,未将对象引用设置到对象的实例
不知道有没有人知道怎么回事,或者分享一些经验,帮我解决这个问题,
我调试得快要抓狂了

解决方案 »

  1.   

    而且加上的话一般运行一段时间才会出现NullReferenceException我要崩溃了!!!
      

  2.   

    可能是allTable未实例化. 就直接调用了它的方法NewRow()
    你可以在这句:
    DataRow newone = allTable.NewRow();
    按F9设断点,按F5运行程序。
    再进行F11单步执行,看到底是哪句抛出的空引用异常.
      

  3.   

    未处理的“System.NullReferenceException”类型的异常出现在 system.windows.forms.dll 中其他信息:未将对象引用设置到对象的实例。_stackTraceString "   at System.Windows.Forms.WndProc.Invoke(IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       at System.Windows.Forms.UnsafeNativeMethods.PeekMessage(MSG& msg, IntPtr hwnd, Int32 msgMin, Int32 msgMax, Int32 remove)
       at System.Windows.Forms.ComponentManager.System.Windows.Forms.UnsafeNativeMethods+IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at GUI.GUI.Main() in d:\work\\gui\form1.cs:line 322" string
    这些异常信息也很难看出什么来
      

  4.   

    是不是添加了很多行,上千?上万?
    如果除上面的错误,应该是DataRow newone = allTable.NewRow();创建失败。
    在后面加一个Assert,Debug.Assert(newone != null);
    如果是创建行失败,那么你的程序就不能一味着添加新的行,好考虑其他处理机制。
      

  5.   

    没有添加很多行,几十行吧有没有可能跟datagrid有关系呢
      

  6.   

    如果不将allTable绑定到DataGrid程序正常但是绑定之后,添加2行之后程序就出问题,怎么办呢,HELP!!!!