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

本文关键字:实例 对象 设置 对象引用 | 更新日期: 2024-05-25 14:48:07

我知道这里有很多类似的问题,但其中一些是在VB中,或者答案不适用于我的代码。现在是:

 private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {

        if(e.ColumnIndex == 7)
        {
            double cellvalue = new double();
            double totalkcal = new double();
            double totalpret = new double();

            double.TryParse(dataGridView1[e.ColumnIndex - 1, e.RowIndex].Value.ToString(), out cellvalue);
            double.TryParse(dataGridView1[e.ColumnIndex - 3, e.RowIndex].Value.ToString(), out totalkcal);
            double.TryParse(dataGridView1[e.ColumnIndex - 4, e.RowIndex].Value.ToString(), out totalpret);
            textBox5.Text = totalkcal.ToString();

        }
    }

它怎么了?每当单击单元格时,我都会在标题中看到错误。

非常感谢!

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

给变量初始值:

double cellvalue = 0;
double totalkcal = 0;
double totalpret = 0;