C#实例化对象为空

时间:2017-04-02 01:36:32

标签: c# object runtime-error

我正在尝试在控制台应用程序中编写一个生命游戏程序,但保留我的实例化单元格对象为空的运行时异常。

我正在循环一个二维数组并创建对象,然后我可视化它们,如果它们还活着的话。当我尝试访问位置时,我一直收到错误

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

当我尝试访问我的对象属性时,即使它们具有值

,也会发生这种情况
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;
    }
}

}

以下是我班级的代码:

    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;
    }

}
    }

1 个答案:

答案 0 :(得分:2)

您正在为窗口中的每个x,y坐标对创建Cell对象,但您不会在Point数组中设置这些单元格对象。

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
        }
    }

这样的事情应该会有所帮助。