菜鸟求救 C# 二维数组初始化问题

BBQish 2013-05-19 10:56:57
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。
...全文
209 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
BBQish 2013-05-19
  • 打赏
  • 举报
回复
引用 5 楼 caozhy 的回复:
estate[,] state = new estate[maxx, maxy]; => state = new estate[maxx, maxy]; this.state[x, y] = new estate(); estate的定义在哪里?
public enum estate { dead, living, born, sick };//(死亡,活着,新生,虚弱) copy的时候copy错了。 estate[,] state = new estate[maxx, maxy]; => state = new estate[maxx, maxy]; 之后就可以了,谢谢你了
  • 打赏
  • 举报
回复
如果你写
    public class   estate
    {
        public static estate  dead{get;set;}
    
        .......
那么这就是一个很平常的bug,自己用点心、自己看比较好。
  • 打赏
  • 举报
回复
贴出你的 estate 类的 dead 属性定义来。
threenewbee 2013-05-19
  • 打赏
  • 举报
回复
estate[,] state = new estate[maxx, maxy]; => state = new estate[maxx, maxy]; this.state[x, y] = new estate(); estate的定义在哪里?
threenewbee 2013-05-19
  • 打赏
  • 举报
回复
你干吗要定义一个和成员变量名字一样的局部变量?
BBQish 2013-05-19
  • 打赏
  • 举报
回复
引用 2 楼 caozhy 的回复:
for (int j = 0; j < maxy; y++) -> for (int j = 0; j < maxy; j++)
换参数的时候忘了改了,现在改回来了,谢谢, 但是问题依旧存在,一模一样。
threenewbee 2013-05-19
  • 打赏
  • 举报
回复
for (int j = 0; j < maxy; y++) -> for (int j = 0; j < maxy; j++)
threenewbee 2013-05-19
  • 打赏
  • 举报
回复
this.state[i, j] = estate.dead;

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧