0

My plan was to have a field of 3*3 with buttons and change the color (which is used to mark a button) by every turn. So i created a playerclass in which the color of the player should be stored and used this string to check, which color should be used in form1. Problem: Null reference exception in the method colorChoice. This is the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TicTacToe
{
    static class Program
    {
        /// <summary>
        ///  The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.SetHighDpiMode(HighDpiMode.SystemAware);
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

using System;
using System.Collections.Generic;
using System.Text;

namespace TicTacToe
{
    public class Player
    {

        public String playerColor = "Red";
        public int[,] storage;

        public Player(String playerColor)
        {
            this.playerColor = playerColor;
        }

    }
}

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 TicTacToe
{
    public partial class Form1 : Form
    {

        public static Player player1;
        public static Player player2;
        public static  Player currentPlayer;
        public Form1()
        {
            InitializeComponent();
            Player player1 = new Player("Red");
            Player player2 = new Player("Blue");
            currentPlayer = player1;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            playerSwitch();
            colorChoice(button1);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            playerSwitch();
            colorChoice(button2);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            playerSwitch();
            colorChoice(button3);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            playerSwitch();
            colorChoice(button4);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            playerSwitch();
            colorChoice(button5);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            playerSwitch();
            colorChoice(button6);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            playerSwitch();
            colorChoice(button7);
        }

        private void button8_Click(object sender, EventArgs e)
        {
            playerSwitch();
            colorChoice(button8);
        }

        private void button9_Click(object sender, EventArgs e)
        {
            playerSwitch();
            colorChoice(button9);
        }

        private void colorChoice(Button button)
        {
            if(currentPlayer.playerColor == "Red") button.BackColor = Color.Red;
            if (currentPlayer.playerColor == "Blue") button.BackColor = Color.Blue;
        }

        public static void playerSwitch()
        {
            if (currentPlayer == player1)
            {
                currentPlayer = player2;
            }
            else //if (currentPlayer == player2)
            {
                currentPlayer = player1;
            }
        }
    }
}
heermaas
  • 5
  • 5
  • In the `Program` class… all the definitions of `player1`, `player2` and `currentPlayer` will be “unknown” to `Form1` unless you pass them to `From1`… I suggest you move the `Player` definitions and `PlayerSwitch` method to the `Form1` class. It belongs there anyway. – JohnG Nov 04 '21 at 09:44
  • Thank you really much for taking look at my code! Now i moved all that stuff over, but the problem stays the same. – heermaas Nov 04 '21 at 09:56
  • When you assign `currentPlayer` at the top of the code… `player1` is `null`. You need to set it after `Player player1 = new Player("Red");` is executed. Also, you need to look closer at `playerSwitch`… test what happens if `currentPlayer = player1`… If the `currentPlayer` is `player1` it will never get switched to `player2`. – JohnG Nov 04 '21 at 10:01
  • Thank you JohnG. Now the worst thing is fixed, but you are right, the player won't get switched. Why wouldn't that fix the problem?: if (currentPlayer == player1) { currentPlayer = player2; } else if(currentPlayer == player2) { currentPlayer = player1; } – heermaas Nov 04 '21 at 10:29
  • And where can I see the output in the console of "Console.WriteLine(...);". Do I have to change a filter in the output-window of Visual Studio? – heermaas Nov 04 '21 at 10:31
  • In the currently posted `playerSwitch` code the `else` portion of the `if` statement is missing. I see it in your comment, but not in your code. The second `if` statement is not really necessary… `if (currentPlayer == player1) { currentPlayer = player2; } else { currentPlayer = player1; }` – JohnG Nov 04 '21 at 10:44
  • _” … where can I see the output in the console of "Console.WriteLine(...);”_ … ? … you are running `WinForms` application, there is no “console.” You normally would output to a control like a `TextBox` or a simple dialog box like… `MessageBox.Show(“Stuff to output”);` – JohnG Nov 04 '21 at 10:44
  • but without the second if there will be again the problem of the beginning (the null reference exception in the colorChoice method) and with it, it still doesn't work (the color does not change). And there really is no other way to test something by giving out text anywhere than using a text box on the screen? Because the textbox cant be used in every method (there has to be a reference - how can i get that?) – heermaas Nov 04 '21 at 11:05
  • _”but without the second if there will be again the problem of the beginning (the null reference exception in the colorChoice method)”_ … ? … why would removing the second `if` statement cause the variable to be `null`? … Please [edit] you question with the updated code. – JohnG Nov 04 '21 at 11:22
  • I don't understand too. It doesn't make any kind of sense in my eyes... That's why I'm still anoying you. Now I've updated the code. – heermaas Nov 04 '21 at 12:48
  • In the forms constructor on the lines… `Player player1 = new Player("Red");` and `Player player2 = new Player("Blue");` … take off the type definition `Player` … this creates a new `Player` object and goes out of scope after the constructor exits. Change the code to… `player1 = new Player("Red");` and `player2 = new Player("Blue");` – JohnG Nov 04 '21 at 13:03
  • Thanks, you helped really much! All problems are solved (for the moment ;D). – heermaas Nov 04 '21 at 14:05

0 Answers0