简体   繁体   中英

C# Instantiated object is null

I am trying to write a game of life program in a console app but keeping the runtime exception that my instantiated cell object is null.

I am looping through a 2d array and creating the objects then I am visualizing them if they are alive. I keep getting an error when I try access the position

(Console.WriteLine(cell.Isalive.ToString());)

It always happens when I try access my objects properties even through they have values

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.IO;
using System.Diagnostics;

namespace Game_of_life
{
class Program
{

    static Random rnd = new Random();

    static void Main(string[] args)
    {


        Console.SetWindowSize(40, 40);


      Cell[,] Points = new Cell[Console.WindowHeight, Console.WindowWidth];
      for(int i = 0; i < Console.WindowHeight; i++)
        {
            for(int j = 0; j < Console.WindowWidth; j++)
            {
                bool Alive = false;
                if (rnd.Next(0, 10) == 1)
                    Alive = true;
                Cell cell = new Cell(i, j, Alive);
            }

        }

        while (true)
        {
            Console.Clear();
            foreach(Cell cell in Points)
            {
                Console.WriteLine(cell.Isalive.ToString());
                Tuple<int, int>[] points = GetNeighbors(new Point(cell.Position.X, cell.Position.Y));

                for(int i = 0; i < 4; i++){
                    if (points[0] != null)
                        cell.Neighbors[i].Position = new Point(points[i].Item1, points[i].Item2);
                }

                if(cell.Isalive == true)
                {
                    Console.SetCursorPosition(cell.Position.X, cell.Position.Y);
                    Console.Write("X");
                }
                System.Threading.Thread.Sleep(500);
            }
        }

    }


    static Tuple<int, int>[] GetNeighbors(Point pos)
    {
        Tuple<int, int>[] points = new Tuple<int, int>[4];
        if (pos.X - 1 > 0) {
            points[0] = Tuple.Create(pos.X - 1, pos.Y);
                }
        else
        {
            points[0] = null;
        }
        if(pos.X + 1< Console.WindowWidth)
        {
            points[1] = Tuple.Create(pos.X + 1, pos.Y);
        }

        if(pos.Y - 1 > 0)
        {
            points[2] = Tuple.Create(pos.X, pos.Y - 1);
        }
        else
        {
            points[0] = null;
        }

        if (pos.Y + 1 < Console.WindowHeight)
        {
            points[3] = Tuple.Create(pos.X, pos.Y + 1);
        }
        else
        {
            points[0] = null;
        }


        return points;
    }
}

}

Here is the code for my class:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Drawing;

    namespace Game_of_life
    {
       class Cell
        {
           //Defines if the cell is currently alive
            private bool isAlive;
            public bool Isalive { get; set; }


    //Neighbors of the cell
    private Cell[] neighnors = new Cell[4];
    public Cell[] Neighbors { get; set; }

    private Point position;
    public Point Position 
        {
        get
        {
            return position;
        }
        set
        {
            position = value;
        }
        }

    private int neighborCount;
    public int NeighborCount { get; set; }



    public Cell (int x , int y, bool Alive)
    {
        this.position = new Point(x, y);
        isAlive = Alive;
    }

}
    }

You're creating Cell objects for every x,y coordinate pair across the window but you aren't ever setting those cell objects in your Point array.

Cell[,] Points = new Cell[Console.WindowHeight, Console.WindowWidth];
    for(int i = 0; i < Console.WindowHeight; i++)
    {
        for(int j = 0; j < Console.WindowWidth; j++)
        {
            bool Alive = false;
            if (rnd.Next(0, 10) == 1)
                Alive = true;
            Cell cell = new Cell(i, j, Alive);
            Points[i, j] = cell; //this is missing
        }
    }

Something like that should help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM