none
null reference exception error in my tic tack toe game mini project RRS feed

  • Question

  • i have a problem with a null reference exception error that happens when my tic tack toe bot picks a square and the whole program crashes and produces a null reference  exception on the if (buttons.Count > 0)

                //this could not work as it produced a null refefance exception 
               // if (buttons.Count > 0)
               // {
               //     int index = rng.Next(buttons.Count);
               //     buttons[index].Enabled = false;
               //     currentPlayers = Players.O;
               //     buttons[index].Text = currentPlayers.ToString();
               //     buttons[index].BackColor = System.Drawing.Color.DarkSalmon;
               // }

    i suspect that the error has to do something with this part of my code but i cant seem to solve it

     private void Playerchoose(object sender, EventArgs e)
            {
                //for user to pick and set a square
                var button = (Button)sender;
                currentPlayers = Players.X;
                button.Text = currentPlayers.ToString();
                button.Enabled = false;
                button.BackColor = System.Drawing.Color.DarkMagenta;
               
                Checkwinner();
                AItimer.Start();
    
    
            }
    this is the rest of my code
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace juncheng_t0030335b_miniproject
    {
        public partial class TicTackToe : Form
        {
            //all the variables used
            int AIscore = 0; 
            int Humanscore = 0;
            Players currentPlayers;
            //Random rng = new Random(); not used because cant fix null refrence exception
            List<Button> buttons;
            public enum Players
            {
                X, O
            }
    
    
            public TicTackToe()
            {
                InitializeComponent();
                Resetgame();
            }
    
            private void Playerchoose(object sender, EventArgs e)
            {
                //for user to pick and set a square
                var button = (Button)sender;
                currentPlayers = Players.X;
                button.Text = currentPlayers.ToString();
                button.Enabled = false;
                button.BackColor = System.Drawing.Color.DarkMagenta;
               
                Checkwinner();
                AItimer.Start();
    
    
            }
    
            private void AIturn(object sender, EventArgs e)
            {
    
                foreach (Control X in this.Controls)
                {
                    //will run when its the AI turn and ai will pick a square to choose
                    //work around for null refrence exception 
                    if (X is Button && X.Text == "?" && X.Enabled)
                    {
                        X.Enabled = false;
                        currentPlayers = Players.O;
                        X.Text = currentPlayers.ToString();
                        X.BackColor = System.Drawing.Color.DarkSalmon;
                        AItimer.Stop();
                        Checkwinner();
                        break;
    
                      
    
    
    
                    }
                    else
                    {
                        AItimer.Stop();
                    }
    
    
    
                }
    
    
    
    
    
                //this could not work as it produced a null refefance exception 
               // if (buttons.Count > 0)
               // {
               //     int index = rng.Next(buttons.Count);
               //     buttons[index].Enabled = false;
               //     currentPlayers = Players.O;
               //     buttons[index].Text = currentPlayers.ToString();
               //     buttons[index].BackColor = System.Drawing.Color.DarkSalmon;
               // }
    
    
            }
    
            private void Playagain(object sender, EventArgs e)
            {
                //call function to reset the game back to default state
                Resetgame();
            }
    
            private void Resetgame()
            {
                foreach (Control X in this.Controls)
                {
                    //reset the entier board back to default
                    if (X is Button && X.Tag == "select")
                    {
                        ((Button)X).Enabled = true;
                        ((Button)X).Text = "?";
                        ((Button)X).BackColor = default(Color);
                    }
                }
            }
    
            private void Intiallizebtn()
            {
                //used to load all buttons to the buttonlist
                buttons = new List<Button> { tictackbtn1, tictackbtn2, tictackbtn3, tictackbtn4, tictackbtn5, tictackbtn6, tictackbtn7, tictackbtn8, tictackbtn9 };
            }
    
            private void Checkwinner()
            {       //check if user win the game and add score
                   if (tictackbtn1.Text == "X" && tictackbtn2.Text == "X" && tictackbtn3.Text == "X"
                    || tictackbtn4.Text == "X" && tictackbtn5.Text == "X" && tictackbtn6.Text == "X"
                    || tictackbtn7.Text == "X" && tictackbtn8.Text == "X" && tictackbtn9.Text == "X"
                    || tictackbtn1.Text == "X" && tictackbtn4.Text == "X" && tictackbtn7.Text == "X"
                    || tictackbtn2.Text == "X" && tictackbtn5.Text == "X" && tictackbtn8.Text == "X"
                    || tictackbtn3.Text == "X" && tictackbtn6.Text == "X" && tictackbtn9.Text == "X"
                    || tictackbtn1.Text == "X" && tictackbtn5.Text == "X" && tictackbtn9.Text == "X"
                    || tictackbtn3.Text == "X" && tictackbtn5.Text == "X" && tictackbtn7.Text == "X")
    
                {
                    AItimer.Stop();
                    Humanscore++;
                    humanplrlbl.Text = "HUMAN: " + Humanscore;
                    Resetgame();
                }
                   //check if AI win the game and add score
                else if (tictackbtn1.Text == "O" && tictackbtn2.Text == "O" && tictackbtn3.Text == "O"
                    || tictackbtn4.Text == "O" && tictackbtn5.Text == "O" && tictackbtn6.Text == "O"
                    || tictackbtn7.Text == "O" && tictackbtn8.Text == "O" && tictackbtn9.Text == "O"
                    || tictackbtn1.Text == "O" && tictackbtn4.Text == "O" && tictackbtn7.Text == "O"
                    || tictackbtn2.Text == "O" && tictackbtn5.Text == "O" && tictackbtn8.Text == "O"
                    || tictackbtn3.Text == "O" && tictackbtn6.Text == "O" && tictackbtn9.Text == "O"
                    || tictackbtn1.Text == "O" && tictackbtn5.Text == "O" && tictackbtn9.Text == "O"
                    || tictackbtn3.Text == "O" && tictackbtn5.Text == "O" && tictackbtn7.Text == "O")
    
                {
    
                    AItimer.Stop();
                    AIscore++;
                    aiplrlbl.Text = "AI: " + AIscore;
                    Resetgame();
    
                }
            }
    
            private void toseletion_Click(object sender, EventArgs e)
            {
                // to go back to game selection form
                this.Hide();
                gameseletion gameseletion = new gameseletion();
                gameseletion.ShowDialog();
            }
    
            private void TicTackToe_Load(object sender, EventArgs e)
            {
    
            }
        }
    }
    

    Thursday, October 18, 2018 1:49 PM

All replies