Object reference undefined, what could it be?

Asked

Viewed 96 times

-2

"Undefined object reference for an object instance."

Button[,] botoes = new Button[(int)numericUpDownLinhas.Value, (int)numericUpDownColunas.Value];
    for(int i = 0; i < (int)numericUpDownLinhas.Value; i++)
    {
        for(int j = 0; j < (int)numericUpDownColunas.Value; j++)
        {
            botoes[i, j].BackColor = button1.BackColor;
            botoes[i, j].Size = new Size((int)numericUpDownX.Value, (int)numericUpDownY.Value);
            botoes[i, j].Location = new Point(18 + j * ((int)numericUpDownX.Value + 4), 35 + i * ((int)numericUpDownY.Value + 4));
            this.Controls.Add(botoes[i, j]);
        }
    }

From the error in these 3 lines:

botoes[i, j].BackColor = button1.BackColor;
botoes[i, j].Size = new Size((int)numericUpDownX.Value, (int)numericUpDownY.Value);
botoes[i, j].Location = new Point(18 + j * ((int)numericUpDownX.Value + 4), 35 + i * ((int)numericUpDownY.Value + 4));

Would anyone know why?

  • The error can only happen in one line, which one is it? Put the error more completely. It is one of three variables used that is not available in this scope.

  • what could it be? only asks to be an undefined object reference for an instance of an object. rsrs you are trying to access the value of a variable that is null.

  • I said of the error in the three lines because first points error in the first, if I comment it, points in the second, etc...

  • But as well the value of the variable ta null if at the beginning of the code I set buttons?

  • Nullpointer is relatively easy to find, when you are debugging and give this exception, just go to the error line and put the mouse on top of the objects and look for which of them this null and needs to be instantiated.

  • I solved already, I thought that when we did the array, everything was already set, but then you have to go setting one in one buttons[i, j] = new Button();

Show 1 more comment

1 answer

0

I decided! Before the three lines mentioned I put the following: botoes[i, j] = new Button();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.