返回是System.NullReferenceException异常!

解决方案 »

  1.   

    把这句 dr = null; 去掉试试
      

  2.   

    改为以下写法会好一些
    private void insertDataTable()
    {
             DataRow dr;
    for(int i=0; i<1000; i++)
    {
    dr = QuotationTable.NewRow(); for(int j=0; j<4; j++)
    {
    dr[j] = (object)i;
    } QuotationTable.Rows.Add(dr);
    }
    MessageBox.Show("OK");
    }
      

  3.   

    与dr = null;好像没有关系的。
    删除掉,运行后还是抛出异常
      

  4.   

    jialiang(DayAndNight):
    谢谢提醒。这对优化有很多的帮助。--------------------
    但程序还是运行不行
      

  5.   

    你在子线程操作的时候,主线程中有没有对Datatable进行操作??
      

  6.   

    using System;
    using System.Data;
    using System.Threading;
    using System.Data.SqlClient;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using dataBase;namespace LanxeSoft
    {
    /// <summary>
    /// Quotation 的摘要说明。
    /// </summary>
    public class Quotation : System.Windows.Forms.Form
    {
    private System.Windows.Forms.DataGrid dgQuotation;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null;
    private IList QuotationItemIndex;
    private IList QuotationItemname;
    private DataTable QuotationTable = new DataTable("QuotationTableList");
    private DataTable reDataTable = null;
    private Thread td; public Quotation(IList QuotationItemIndex, IList QuotationItemname)
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    this.QuotationItemIndex = QuotationItemIndex;
    this.QuotationItemname = QuotationItemname;
    CreatQuotationTableStruction();
    } private void CreatQuotationTableStruction()
    {
    if(QuotationItemIndex.Count == 0 || QuotationItemname.Count == 0)
    {
    MessageBox.Show("建立报表结构出现参数不全错误",getDataBase.MessageBoxShowTitle,MessageBoxButtons.OK,MessageBoxIcon.Warning);
    this.Dispose();
    }
    for(int i=0; i<QuotationItemname.Count; i++)
    {
    QuotationTable.Columns.Add(new DataColumn((string)QuotationItemname[i],typeof(string)));
    } dgQuotation.DataSource = QuotationTable;
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if(components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Quotation));
    this.dgQuotation = new System.Windows.Forms.DataGrid();
    ((System.ComponentModel.ISupportInitialize)(this.dgQuotation)).BeginInit();
    this.SuspendLayout();
    // 
    // dgQuotation
    // 
    this.dgQuotation.CaptionVisible = false;
    this.dgQuotation.DataMember = "";
    this.dgQuotation.Dock = System.Windows.Forms.DockStyle.Fill;
    this.dgQuotation.HeaderForeColor = System.Drawing.SystemColors.ControlText;
    this.dgQuotation.Location = new System.Drawing.Point(0, 0);
    this.dgQuotation.Name = "dgQuotation";
    this.dgQuotation.Size = new System.Drawing.Size(640, 349);
    this.dgQuotation.TabIndex = 0;
    // 
    // Quotation
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(640, 349);
    this.Controls.Add(this.dgQuotation);
    this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
    this.Name = "Quotation";
    this.Text = "Quotation";
    ((System.ComponentModel.ISupportInitialize)(this.dgQuotation)).EndInit();
    this.ResumeLayout(false); }
    #endregion public void AddNewQuotationProduct()
    {
    td = new Thread(new ThreadStart(insertDataTable));
    td.Name = "tdQuotation";
    td.Start();
    } private void insertDataTable()
    {
    DataRow dr;
    for(int i=0; i<1000; i++)
    {
    dr = QuotationTable.NewRow(); for(int j=0; j<4; j++)
    {
    dr[j] = (object)i;
    } QuotationTable.Rows.Add(dr);
    }
    MessageBox.Show("OK");
    } }
    }
    这是全部代码
      

  7.   

    这是用到的部分(删除不要的)
    private System.Windows.Forms.DataGrid dgQuotation;
    private IList QuotationItemIndex;
    private IList QuotationItemname;
    private DataTable QuotationTable = new DataTable("QuotationTableList");
    private DataTable reDataTable = null;
    private Thread td;public Quotation(IList QuotationItemIndex, IList QuotationItemname)
    {
    InitializeComponent(); this.QuotationItemIndex = QuotationItemIndex;
    this.QuotationItemname = QuotationItemname;
    CreatQuotationTableStruction();
    }private void CreatQuotationTableStruction()
    {
    if(QuotationItemIndex.Count == 0 || QuotationItemname.Count == 0)
    {
    MessageBox.Show("建立报表结构出现参数不全错误",getDataBase.MessageBoxShowTitle,MessageBoxButtons.OK,MessageBoxIcon.Warning);
    this.Dispose();
    }
    for(int i=0; i<QuotationItemname.Count; i++)
    {
    QuotationTable.Columns.Add(new DataColumn((string)QuotationItemname[i],typeof(string)));
    } dgQuotation.DataSource = QuotationTable;
    }public void AddNewQuotationProduct()
    {
    td = new Thread(new ThreadStart(insertDataTable));
    td.Name = "tdQuotation";
    td.Start();
    }private void insertDataTable()
    {
    DataRow dr;
    for(int i=0; i<1000; i++)
    {
    dr = QuotationTable.NewRow(); for(int j=0; j<4; j++)
    {
    dr[j] = (object)i;
    } QuotationTable.Rows.Add(dr);
    }
    MessageBox.Show("OK");
    }
      

  8.   

    如果我把dgQuotation.DataSource = QuotationTable;注释掉,就没有问题了。程序大概运行到14条就抛出异常了