class Cells    
{
        private int maxx;
        private int maxy;
        private int x;//数组坐标X
        private int y;//数组坐标Y
        private estate[,] state;//状态
         public enum eshape { square, circle, custom };//(正方形,圆形,自定义)

public Cells() //构造函数        
{
            maxx = 1;
            maxy = 1;
            x = 1;
            y = 1;
            estate[,] state = new estate[maxx, maxy];
             for (int i = 0; i < maxx; i++)
                for (int j = 0; j < maxy; y++)
                    this.state[x, y] = estate.dead;(该行出错)

异常:
未处理NullReferenceException
未将对象引用设置到对象的实例。
疑难解答提示:
使用“new”关键字创建对象实例。
在调用方法前,通过检查对象是否为null。C#二维数组异常实例对象

解决方案 »

  1.   

     this.state[i, j] = estate.dead;
      

  2.   

    for (int j = 0; j < maxy; y++)
    ->
    for (int j = 0; j < maxy; j++)
      

  3.   

    estate[,] state = new estate[maxx, maxy];
    =>
    state = new estate[maxx, maxy];this.state[x, y] = new estate();
    estate的定义在哪里?
      

  4.   

    贴出你的 estate 类的 dead 属性定义来。
      

  5.   

    如果你写    public class   estate
        {
            public static estate  dead{get;set;}
        
            .......那么这就是一个很平常的bug,自己用点心、自己看比较好。
      

  6.   

     public enum estate { dead, living, born, sick };//(死亡,活着,新生,虚弱)
    copy的时候copy错了。
    estate[,] state = new estate[maxx, maxy];
    =>
    state = new estate[maxx, maxy];之后就可以了,谢谢你了