var gridToXml=new ClassVivod[dataGridView1.Rows.Count]; for (int i = 0; i < gridToXml.Count(); i++) { gridToXml[i].Key = dataGridView1.Rows[i].Cells["Key"].Value.ToString(); gridToXml[i].ValueXML_1 = dataGridView1.Rows[i].Cells["Russian"].Value.ToString(); gridToXml[i].ValueXML_2 = dataGridView1.Rows[i].Cells[comboBox1.Text].Value.ToString(); } 

At the time of assignment an error occurs, the object reference does not indicate an object instance.

  • You probably programmed only in C ++ before? In C #, an array contains not objects, but references to objects, which means that it also needs to be initialized with empty objects. - VladD

1 answer 1

If this is all code, then this line (like the following ones)

 gridToXml[i].Key = dataGridView1.Rows[i].Cells["Key"].Value.ToString(); 

attempts to assign the Key property of the object located in gridToXml[i] to the value to the right of the equal sign. At this moment, of course, in gridToXml[i] there is nothing but null , since first, the object must be initialized and placed in gridToXml[i] :

  for (int i = 0; i < gridToXml.Count(); i++) { gridToXml[i] = new ClassVivod(); ... 
  • thank you! - alirasul