I'm getting a null reference exception and i don't know how to solve it or why it even happens.
private void editThisToolStripMenuItem_Click(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count <= 1)
{
Form2 f2 = new Form2(dataGridView1.SelectedRows[0].Cells[1].Value.ToString(), Convert.ToInt32(dataGridView1.SelectedRows[0].Cells[2].Value));
f2.ShowDialog();
textBox2.Text = textBox1.Text.Replace(f2.oldtext, f2.newtext);
this.dataGridView1.SelectedRows[0].Cells[3].Value = f2.newtext;
this.dataGridView1.SelectedRows[0].Cells[3].Style.BackColor = Color.IndianRed;
}
else
{
ONOType[] ono = new ONOType[this.dataGridView1.SelectedRows.Count];
int indexerr = 0;
foreach (DataGridViewRow r in dataGridView1.SelectedRows)
{
ono[indexerr].newtext = this.dataGridView1.SelectedRows[indexerr].Cells[3].Value.ToString(); //null expection at ono[indexerr].newtext
ono[indexerr].oldtext = this.dataGridView1.SelectedRows[indexerr].Cells[1].Value.ToString();
ono[indexerr].offset = Convert.ToInt32(dataGridView1.SelectedRows[indexerr].Cells[0].Value);
indexerr++;
}
Form3 f3 = new Form3(ono);
f3.ShowDialog();
indexerr = 0;
for (int i = 0; i < dataGridView1.SelectedRows.Count; i++)
{
textBox2.Text = textBox1.Text.Replace(f3.nt[i].oldtext, f3.nt[i].newtext);
this.dataGridView1.SelectedRows[i].Cells[3].Value = f3.nt[i].newtext;
this.dataGridView1.SelectedRows[i].Cells[3].Style.BackColor = Color.IndianRed;
}
}
}
and here is the ono class
namespace IEditor
{
public class ONOType
{
public string oldtext { get; set; }
public string newtext { get; set; }
public int offset { get; set; }
}
}
The problem starts at:
ONOType[] ono = new ONOType[this.dataGridView1.SelectedRows.Count];
It defines the new array of this class type all to null which something I don't want and maybe it's caused by the keyword "new" and without the "new" keyword i get comp. error for assigning value to that objects in this array.
What i did try was adding a ctor to this class to assign default values for each of this array members of members (aka assign values for oldtext/newtext/offset) on deceleration but still objects in this objects array are null and i did try to do the same at the get/set properties , but i still failed.
Please add an explanation with the solution.
SelectedRows.Count <= 1did you meanSelectedRows.Count >= 1?