我的问题将是相当一致的。
我有一个泛型类Cell<T>。我有一个客户端类,它可以操作Cell<T>的二维数组
public sealed class Environment
{
private Cell<YUV>[,] primaryLayer;
public Colorizator(Cell<YUV>[,] layer)
{
// initialization...
}
// ...
}在Environment类中,我调用的方法如下:
for(var ix = 1; ix < this.Width - 1; ix++)
{
for(var iy = 1; iy < this.Height - 1; iy++)
{
this.primaryLayer[ix, iy].Window[0] = this.primaryLayer[ix - 1, iy + 1];
this.primaryLayer[ix, iy].Window[1] = this.primaryLayer[ix, iy + 1];
this.primaryLayer[ix, iy].Window[2] = this.primaryLayer[ix + 1, iy + 1];
this.primaryLayer[ix, iy].Window[3] = this.primaryLayer[ix + 1, iy];
this.primaryLayer[ix, iy].Window[4] = this.primaryLayer[ix + 1, iy - 1];
this.primaryLayer[ix, iy].Window[5] = this.primaryLayer[ix, iy - 1];
this.primaryLayer[ix, iy].Window[6] = this.primaryLayer[ix - 1, iy - 1];
this.primaryLayer[ix, iy].Window[7] = this.primaryLayer[ix - 1, iy];
}
}它用邻居填充每个单元格摩尔邻里。
然后我在其他方法中使用类似于泛洪填充的算法:
foreach(var cell in some_list)
{
Parent = cell;
Parent.Conquer(Parent);
Parent.ViabilityRatio = this.ViabilityTresholds.Max;
lr[Parent.X, Parent.Y] = Parent;
while(true)
{
if(null == (Child = Parent.Window.FirstOrDefault(x => !(x as Cell<YUV>).IsMarked) as Cell<YUV>))
{
break;
}
else
{
Parent.Mark(Child);
var wt = default(Double);
foreach(Cell<YUV> ch in Child.Window)
{
// Operation fails
// NullReferenceException occurs: there's no cells in `Child` neighbourhood
wt += ch.ViabilityRatio;
}
Parent = Child;
}
}
} 当我尝试迭代Child.Window时,我发现没有邻域元素。首先,我认为Parent和Child,尤其是Child,不会保存对我分配给它们的对象的引用。我的意思是,cell变量in the foreach loop具有非空邻域。但父母却没有。
解决方案1.帮助处理父单元格
我在Cell<T>类中实现了Copy方法:
public Cell<T> Copy()
{
return (Cell<T>)this.MemberwiseClone();
} 从那时起,Parent保存了它的非空Window属性。
foreach(var cell in some_list)
{
Parent = cell.Copy();但不幸的是,if(null == (Child = Parent.Window.FirstOrDefault(x => !(x as Cell<YUV>).IsConquered) as Cell<YUV>).Copy())试验失败了。
Child没有不为空的Window属性。
求求你,救命!谢谢!
编辑
我试着做下一步:
var ActiveWindow = (from ch in Parent.Window select (Cell<YUV>)ch).ToList(); ActiveWindow集合将保持所有邻居的初始化。此外,正如我一直期望的那样,所有邻居都初始化了自己的邻居。
但是当我调试这个的时候:Child = ActiveWindow[0] ... Child没有存储邻居..
编辑2
Child单元格不为空。我在else子句中遇到异常。
这是我用我的Child手机得到的:

这是我用Parent细胞得到的结果:

发布于 2011-06-07 16:29:37
我希望我没有弄错,这些话让我的眼睛很痛。
假设Parent.Window不包含任何未被征服的Window,那么
Parent.Window.FirstOrDefault(x => !(x as Cell<YUV>).IsConquered) as Cell<YUV>
由于没有与模式匹配的项,因此没有First,并且default(Cell<YUV>)返回null。
重新考虑这句话:
if (null == (Child = Parent.Window.FirstOrDefault(x => !(x as Cell<YUV>).IsConquered) as Cell<YUV>).Copy())
将会产生以下结果:
if (null == (Child = null).Copy())
或者:
if (null == null.Copy())
因为Copy()方法没有对象引用,所以会抛出异常。
https://stackoverflow.com/questions/6262001
复制相似问题