0

I'm implementing a minesweeper game using winform. But, I got System.NullReferenceException. I think I've put the values ​​in BTN[] properly, But in another function, the debugger said BTN[] is null.

This is the problematic part of my code. and I've commented out the problematic part.

public Form_play(int size)
    {
        init(size);

        int top = 5;
        int left = 5;

        for (int j = 0; j < size; j++)
        {
            for (int i = 0; i < size; i++)
            {
                Button btn = new Button();
                btn.Size = new Size(20, 20);
                btn.Top = top;
                btn.Left = left;
                left += 25;
                this.Controls.Add(btn);

                btn.Name = ((j * size) + i).ToString();

                BTN = new Button[size*size];
                btn.Click += new EventHandler(btn_Click);
                btn.MouseDown += new MouseEventHandler(btn_right);
                BTN[(j * size) + i] = btn;       
                // I've put the values ​​in BTN[]
            }
            top += 25;
            left = 5;
        }
    }


 public void view_num(int n)
    {
        info[n].click = true;
        BTN[n].FlatStyle = FlatStyle.Flat;

        if (info[n].bomb == true)
            BTN[n].Text = "●";
        else if (info[n].num == 0)
            view_zero(n);
        else
            BTN[n].Text = Convert.ToString(info[n].num);
            // But, in here, i got null System.NullReferenceException, because BTN[] is null 
    }
you
  • 1
  • 1

0 Answers0