-1

I'm not able to change the text within any of my buttons in my c# form, I'm making a large tic tac toe game for a class project and just can't seem to find out what my problem is.

Button [,] all = new Button[9,9];
int turn = 1;

public StartingForm()
    {
        InitializeComponent();   
    }

    private void StartingForm_Load(object sender, EventArgs e)
    {
        a00 = all[0, 0];   
        a01 = all[0, 1];
        a02 = all[0, 2];
        a03 = all[0, 3];
        a04 = all[0, 4];
        a05 = all[0, 5];
        a06 = all[0, 6];
        a07 = all[0, 7];
        a08 = all[0, 8];
        a10 = all[1, 0];
        a11 = all[1, 1];
        a12 = all[1, 2];
        a13 = all[1, 3];
        a14 = all[1, 4];
        a15 = all[1, 5];
        a16 = all[1, 6];
        a17 = all[1, 7];
        a18 = all[1, 8];
        a20 = all[2, 0];
        a21 = all[2, 1];
        a22 = all[2, 2];
        a23 = all[2, 3];
        a24 = all[2, 4];
        a25 = all[2, 5];
        a26 = all[2, 6];
        a27 = all[2, 7];
        a28 = all[2, 8];
        a30 = all[3, 0];
        a31 = all[3, 1];
        a32 = all[3, 2];
        a33 = all[3, 3];
        a34 = all[3, 4];
        a35 = all[3, 5];
        a36 = all[3, 6];
        a37 = all[3, 7];
        a38 = all[3, 8];
        a40 = all[4, 0];
        a41 = all[4, 1];
        a42 = all[4, 2];
        a43 = all[4, 3];
        a44 = all[4, 4];
        a45 = all[4, 5];
        a46 = all[4, 6];
        a47 = all[4, 7];
        a48 = all[4, 8];
        a50 = all[5, 0];
        a51 = all[5, 1];
        a52 = all[5, 2];
        a53 = all[5, 3];
        a54 = all[5, 4];
        a55 = all[5, 5];
        a56 = all[5, 6];
        a57 = all[5, 7];
        a58 = all[5, 8];
        a60 = all[6, 0];
        a61 = all[6, 1];
        a62 = all[6, 2];
        a63 = all[6, 3];
        a64 = all[6, 4];
        a65 = all[6, 5];
        a66 = all[6, 6];
        a67 = all[6, 7];
        a68 = all[6, 8];
        a70 = all[7, 0];
        a71 = all[7, 1];
        a72 = all[7, 2];
        a73 = all[7, 3];
        a74 = all[7, 4];
        a75 = all[7, 5];
        a76 = all[7, 6];
        a77 = all[7, 7];
        a78 = all[7, 8];
        a80 = all[8, 0];
        a81 = all[8, 1];
        a82 = all[8, 2];
        a83 = all[8, 3];
        a84 = all[8, 4];
        a85 = all[8, 5];
        a86 = all[8, 6];
        a87 = all[8, 7];
        a88 = all[8, 8];
    }

    private void a00_Click(object sender, EventArgs e)
    {
        if (turn == 1)   
        {
            all[0, 0].Text = "X"; 
            turn = 2;
        }
        else
        {
            all[0, 0].Text = "O";
            turn = 1;   //this makes it X's turn
        }
    }

Whenever I try clicking the button I get this error

An unhandled exception of type 'System.NullReferenceException' occurred in Super TTT.exe Additional information: Object reference not set to an instance of an object.

All my buttons are null and I can't find a way to change them (also sorry in advance if I didn't provide enough info, I'm kinda new to this).

Brian Rogers
  • 110,187
  • 27
  • 262
  • 261
Andrew
  • 3
  • 1
  • 2
    You created the array with "new Button[9,9];" but never actually created the items at each index. When you access them all[#, #] you are just pulling out a null value rather than a button. – bgura Jan 03 '17 at 23:12
  • I'm curious, why do you load each element in the array into separate fields? What's the point? –  Jan 03 '17 at 23:17

2 Answers2

1
Button [,] all = new Button[9,9];

This line in your code creates only array of elements, not buttons itself. You have to iterate over it and invoke constructor for every button.

for(int i = 0; i < 9; i++)
for(int j = 0; j < 9; j++) all[i,j] = new Button();

Okay, one problem solved but there will be many others with your current implementation...

Krzysztof
  • 457
  • 4
  • 10
1

Here:

Button [,] all = new Button[9,9];

... all you have done is initialize an array of Button. Each element of the array is null.

If you debug through the StartingForm_Load(...) method you should be able to see each assignment (ex: a00 = all[0, 0]) results in a null value.

Add an element initialization to the StartingForm constructor:

public StartingForm() {
    InitializeComponent(); 

    for(int x = 0; x < 9; x++)
    for(int y = 0; y < 9; y++) {
        // note: after initialization, you will need to assign position values
        all[x, y] = new Button();
    }  
}

I am assuming that you are using WinForms. If you want to continue with your current implementation then I would suggest using a TableLayoutPanel.

IAbstract
  • 18,848
  • 14
  • 81
  • 137