-6

The purpose of the code below is to sum up all the index 3 of a datagridview that would continue on adding endlessly in an order system and display it in a label named Price.

private void Add_Click(object sender, EventArgs e)
{
    dgvSelected.Rows.Add(dgvItem.SelectedCells[0].Value.ToString(),
                         dgvItem.SelectedCells[1].Value.ToString(), 
                         dgvItem.SelectedCells[2].Value.ToString(), 
                         dgvItem.SelectedCells[3].Value.ToString());

    for (int i = 0; i < dgvSelected.Rows.Count; i++)
    {
        price = price + int.Parse(dgvSelected.Rows[i].Cells[3].Value.ToString());
        Price.Text = price.ToString();
    }       
}

The problem is that it keeps saying:

"Object reference not set to an instance of an object."

...as an error for the following line:

price = price + int.Parse(dgvSelected.Rows[i].Cells[3].Value.ToString());

There is supposed to be no null in the table but the error keeps on insisting that there is a null. There are multiple rows in the table but the only row it can read is the 1st row. When it comes to the 2nd row which has values, it then will say the error concerned.

gunr2171
  • 16,104
  • 25
  • 61
  • 88
  • 25
    The programming language is almost always right. – BoltClock Sep 07 '11 at 11:40
  • the error occurs when dgvSelected.Rows[i].Cells[3].Value.ToString() i = 1 and above. – erwin mendoza Sep 07 '11 at 11:42
  • Am I wrong or are you adding only one row (index=0)? – Jan Christian Selke Sep 07 '11 at 11:44
  • Looks to me like either `Cells[3]` is null or `.Value` is null. Have you run this in a debug to verify that the value exists? – Joel Etherton Sep 07 '11 at 11:45
  • @erwin: I think you should change the title of your question to avoid all those downvotes. – Jakob Christensen Sep 07 '11 at 11:53
  • 1
    @Erwin fyi if you had titled this post "what am i doing wrong" it probably would not have been down voted. Even if you are 99% sure that you are correct and the compiler/runtime/class library etc has a bug, it is still probably you. – Ben Robinson Sep 07 '11 at 11:55
  • Slightly OT: Don't use a Dgv as a datastructure. This is a poor design and when you solve this issue you'll just run into the next problem. – H H Sep 07 '11 at 12:09
  • 2
    possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – gunr2171 Oct 27 '14 at 19:55

4 Answers4

6

Of course the programming languge isnt wrong. Its reporting to you that you are tring to use an object reference which is in fact null.

In the section dgvSelected.Rows[i].Cells[3].Value.ToString() one of the following is null

  • dgvSelected
  • dgvSelected.Rows
  • dgvSelected.Rows[i]
  • dgvSelected.Rows[i].Cells
  • dgvSelected.Rows[i].Cells[3]
  • dgvSelected.Rows[i].Cells[3].Value

As you can see, you should be doing alot of checking to make sure none of these objets is null

Edit: Strikeout some of those, as rightfully pointed out in comments the loop would not be entered if those were null.

Jamiec
  • 133,658
  • 13
  • 134
  • 193
1

It's you, on this line something is null.

price = price + int.Parse(dgvSelected.Rows[i].Cells[3].Value.ToString())

It is almost certainly that there is not a dgvSelected.Rows[i].Cells[3] for the particular value of i that throws the exception. Put a breakpoint on that line and check dgvSelected.Rows[i].Cells.Length.

Ben Robinson
  • 21,601
  • 5
  • 62
  • 79
0

You are adding only one row to dgvSelected. I assume, there are other rows in there, added somewhere else with less columns. Check all places where you access dgvSelected and make sure no rows with less than 4 columns are added.
You can simply use a debugger to check what exactly the problem is.

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
0

Since, others have already given a 'broad' answer. I would suggest a slimmer 'possible' answer.

Append the below lines of code as the first line inside your loop, and the console output would give you the correst solution.

if(dgvSelected.Rows[i].Cells[3].Value==null){
   Console.Writeline(String.Format("Row Index:{0}, Column Index:3 has an unexpected null value. Ignoring this row.", i)
   continue;
}

Because, looking at your code, it seems less likely that Row/column object would be null. All the best.

Manish Basantani
  • 16,931
  • 22
  • 71
  • 103
  • 1
    If `dgvSelected.Rows[i].Cells[3]` is null then this will throw a NullReferenceException as there is no `Cells[3].Value` so you can't check if it is null. – Ben Robinson Sep 07 '11 at 11:57