i am having trouble with my code, and i am getting System.NullReferenceException

elias 20 Reputation points
2024-01-09T15:33:46.03+00:00

i am trying to make a checkers game and i am experiencing a issue which apparently is due to something being null, i have tried various things and nothing fixes it please help.

here is 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 Checkersgame
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        int n;
        PictureBox[,] P;
        Random random = new Random(); //colour randomisation
        string color = "r", k = "", B1 = "", B2 = "";

        private void Form1_Load(object sender, EventArgs e)
        {
            n = 8;
            P = new PictureBox[n, n]; // 8 by 8 board
            int left = 2, top = 2;
            Color[] colours = new Color[] { Color.White, Color.Black }; // colour array
            for(int i = 0; i<n; i++) // for 8 times //generating a matrix of picture boxes
            {
                left = 2;
                if (i % 2 == 0)
                { 
                    colours[0] = Color.White;
                    colours[1] = Color.Black;
                }
                else 
                {
                    colours[0] = Color.Black;
                    colours[1] = Color.White;
                
                }
                for (int j = 0; j<n; j++) //this makes it pass left to right from the board line by line
                {
                    P[i, j] = new PictureBox(); //new Picturebox
                    P[i, j].BackColor = colours[(j % 2 == 0) ? 1 : 0]; //picture box colour
                    P[i,j].Location = new Point(left, top); //sets the location to the iterated left and top
                    P[i, j].Size = new Size(60, 60); //resizes box
                    left += 60; //moves the next row
                    P[i, j].Name = i + "" + j; //names each box
                    if (i < (n / 2) - 1 && P[i, j].BackColor == Color.Black) //sets the boxes image to the red checker
                    {
                        P[i, j].Image = Properties.Resources.tttAsset_7; //set the boxes image to the red checker
                        P[i, j].Name += " r";// names it accordingly
                    }
                    else if(i >(n/2) && P[i,j].BackColor == Color.Black)  //sets the boxes image to the black checker
                    { 
                        P[i,j].Image = Properties.Resources.tttAsset_6;// sets to property
                        P[i, j].Name += " b";// names it accordingly
                    }
                    P[i, j].SizeMode = PictureBoxSizeMode.Zoom; //scales the size of the checkers peices
                    P[i, j].MouseHover += (sender2, e2) => //when the mouse hovers in the pannel it tracks its coordinates
                    {
                        PictureBox p = sender2 as PictureBox;
                        if (p.Image != null) p.BackColor = Color.FromArgb(255, 64, 64, 64);
                    };
                    P[i, j].MouseLeave += (sender2, e2) => //when the mouse leaves the box
                    {
                        PictureBox p = sender2 as PictureBox;
                        if (p.Image != null) p.BackColor = Color.Black; //returns to black
                    };

                    P[1, j].Click += (sender3, e3) =>
                    {
                        PictureBox p = sender3 as PictureBox; //new picture

                        if (p.Image != null) // Check if PictureBox and its Image are not null
                        {
                            int c = -1, x, y;

                            F();

                            if (p.Name.Split(' ')[2] == color)
                            {
                                x = Convert.ToInt32(p.Name.Split(' ')[0]);
                                y = Convert.ToInt32(p.Name.Split(' ')[1]);
                                k = p.Name;
                                if (p.Name.Split(' ')[2] == "r") c = 1;
                                try
                                {

                                    if (P[x + c, y + 1].Image == null) //lets us move diagonally to the right
                                    {
                                        P[x + c, y + 1].Image = Properties.Resources.tttAsset_6;
                                        P[x + c, y + 1].Name = (x + c) + " " + (y + 1) + " b";
                                        B1 = (x + c) + " " + (y + 1);
                                    }
                                }
                                catch { }
                                try
                                {
                                    if (P[x + c, y - 1].Image == null) //lets us move to the left diagonally
                                    {
                                        P[x + c, y - 1].Image = Properties.Resources.tttAsset_6;
                                        P[x + c, y - 1].Name = (x + c) + "" + (y - 1) + " b";
                                        B1 = (x + c) + " " + (y - 1);
                                    }
                                }
                                catch { }
                            }

                        }


                    };
                    G.Controls.Add(P[i, j]); //adds each picture box to the panel

                }
                top += 60;// move to next row

            }
        }

        private void Form1_MouseLeave(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }
        public void F() //this function will hide the ring if it is taken
        {
            if(B1 != "")
            {
                int x, y;
                x = Convert.ToInt32(B1.Split(' ')[0]);
                y = Convert.ToInt32(B1.Split(' ')[1]);
                P[x, y].Image = null;

            }
            if (B2 != "")
            {
                int x, y;
                x = Convert.ToInt32(B2.Split(' ')[0]);
                y = Convert.ToInt32(B2.Split(' ')[1]);
                P[x, y].Image = null;
            }
        }
    }
}

where the problem is:
`
User's image

User's image

i would really appreciate any help thanks.

Developer technologies | Visual Studio | Other
Developer technologies | C#
{count} votes

Accepted answer
  1. P a u l 10,761 Reputation points
    2024-01-09T15:45:42.1+00:00

    I think it's because the code assumes there's a two spaces in the Name property:

    if (p.Name.Split(' ')[2] == color)
    

    but there's no space in between the i and j:

    P[i, j].Name = i + "" + j;
    

    and then beneath it you're possibly concatenating another character prefixed by a space.

    That might not be the cause of the issue but you'll likely get an index out of bounds error at some point.

    Also is this meant to be P[1, j] and not P[i, j]?

    P[1, j].Click += (sender3, e3)
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.