0

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.

6
  • SelectedRows.Count <= 1 did you mean SelectedRows.Count >= 1? Commented Mar 15, 2013 at 0:21
  • 1
    no , i meant selected rows count <= 1 , it means if the selected items are only one or less to apply the first code which edit only 1 object but the problem is at the multiple selections which to why i'm having that problem at the array of objects Commented Mar 15, 2013 at 0:23
  • If your selected count is zero you then go on to read the first selected item, which is going to be null, was my point. Commented Mar 15, 2013 at 0:25
  • 1
    yes that's right , thank you :) but that doesn't cause problems for now and ill add more checks when i get it to work :) Commented Mar 15, 2013 at 0:27
  • There are so many referneces that can be null in the code. You need to debug it to find the exact place. Enable the "thrown" option for managed exception and run it through the debugger. Commented Mar 15, 2013 at 2:04

1 Answer 1

3

You are creating a new array of ONOType references with:

    ONOType[] ono = new ONOType[this.dataGridView1.SelectedRows.Count];

But none of the actual ONOType objects are created. It's just an array of variables that reference nothing yet.

When you try to assign ono[indexerr].newtext the element at ono[indexerr] is a null reference.

If you did:

    ono[indexerr] = new ONOType();
    ono[indexerr].newtext = this.dataGridView1.SelectedRows[indexerr].Cells[3].Value.ToString();

I think it would work.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for explaining that point to me :) it worked out , thank you so much :)
I wish I could up vote this a thousand times. Literally been trying to figure this out for three days :[

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.