Click here to Skip to main content
15,883,999 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I want to DFS(Depth first search) my graph and add the number into Queue.

So I have to access the nodes in same column (in DataGridView3) and put them in Queue(DataGridView5). If it has seen before then update them(line **//).

I have a problem in line(**//), the error is: "Object refrence not sent to an instance of an object."
plzPlease help me with this problem.tnx Thanks

C#
int a = int.Parse(textBox1.Text);
int b = int.Parse(textBox2.Text);
 
dataGridView1.ColumnCount = a + 1;
dataGridView1.RowCount = b + 1;
 
dataGridView3.ColumnCount = (a * b) + 3;
dataGridView3.RowCount = (a * b) + 3;
 
dataGridView5.ColumnCount = (a * b) + 2;
dataGridView5.RowCount = 2;
 
for (int i = 0; i < dataGridView5.ColumnCount - 1; i++)
{
   for (int k = 2; k < dataGridView3.RowCount; k++)
   {
      for (int L = 2; L < dataGridView3.ColumnCount; L++)
      {
         int p = 0;
 
         for (int f = 0; f <= dataGridView1.RowCount - 1; f++)
         {
              if (k >= dataGridView3.RowCount)
                                break;
            dataGridView5.Rows[1].Cells[i].Value = dataGridView3.Rows[k].Cells[L + p].Value;
 
           
            p = p + dataGridView1.ColumnCount - 1;
 
            if (dataGridView5.Rows[1].Cells[i].Value != "0")
 
                   **//dataGridView5.Rows[1].Cells[i].Value=int.Parse(dataGridView5.Rows[1].Cells[i].Value.ToString()) +int.Parse( dataGridView5.Rows[1].Cells[i-1].Value.ToString());
                            
         }
      }
   }
}
Posted
Updated 7-May-14 23:52pm
v3
Comments
syed shanu 8-May-14 5:32am    
use break point and check the value

1 solution

The first time round these loops, what is the value of i?

Simple:
C#
for (int i = 0; i ...
So teh first time round the loop, if this test works:
C#
if (dataGridView5.Rows[1].Cells[i].Value != "0")
Then
this access will be guaranteed to fail:
C#
... +int.Parse( dataGridView5.Rows[1].Cells[i-1].Value.ToString());
as arrays cannot have negative indexes...
 
Share this answer
 
Comments
CPallini 8-May-14 5:53am    
My 5 for 'teh'.
:-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900