首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么我在尝试给属性数组设置值时会得到NullReferenceException?

为什么我在尝试给属性数组设置值时会得到NullReferenceException?
EN

Stack Overflow用户
提问于 2016-04-10 08:12:25
回答 1查看 46关注 0票数 0

我正在尝试为我的论文做一个遗传算法的实现。主要有两类:作为染色体的Facility和作为基因的FacilityCell。但我在从Facility类获取适合度值时遇到错误。

所需的值在Form.cs中设置,运行算法后,这些属性在Facility实例中为null。这些属性是Facility.FlowsFacility.Demands。我不明白为什么。请帮帮忙。

来自Form.cs的代码部分

代码语言:javascript
运行
复制
fac = new Facility();
List<FacilityCell> gens = new List<FacilityCell>();
for (int i = 0; i < 6; i++)
{
    gens.Add(new FacilityCell(i.ToString(), i));
}

fac.Genes = gens.ToArray();
fac.Cells = gens.ToArray();

float[] dems = new float[3];
dems[0] = 300;
dems[1] = 60;
dems[2] = 160;
fac.Demands = dems;

FacilityCell[][] fl = new FacilityCell[3][];
fl[0] = new FacilityCell[] { 
    fac.Cells[0],
    fac.Cells[2],
    fac.Cells[4],
    fac.Cells[1],
    fac.Cells[3],
    fac.Cells[5] };
fl[1] = new FacilityCell[] {
    fac.Cells[2], 
    fac.Cells[4], 
    fac.Cells[1], 
    fac.Cells[5], 
    fac.Cells[3], 
    fac.Cells[4] };
fl[2] = new FacilityCell[] { 
    fac.Cells[1], 
    fac.Cells[0], 
    fac.Cells[4], 
    fac.Cells[2], 
    fac.Cells[3], 
    fac.Cells[5] };
fac.Flows = fl;

来自Facility.cs的代码

代码语言:javascript
运行
复制
public class Facility : IChromosome
{
    public Facility()
    {

    }
    public Facility(FacilityCell[] cells)
    {
        this.cells = cells;
        flows = null;
        demands = null;
        for (int i = 0; i < cells.Length; i++)
        {
            cells[i].Order = i;
        }
    }

    private IGene[] cells;
    private float[] demands;
    private FacilityCell[][] flows;

    public FacilityCell[][] Flows
    {
        get { return flows; }
        set { flows = value; }
    }
    public FacilityCell[] Cells
    {
        get
        {
            return cells as FacilityCell[];
        }
        set
        {
            cells = value;
        }
    }

    public float[] Demands
    {
        get { return demands; }
        set { demands = value; }
    }

    public float FitValue
    {
        get
        {
            float total = 0;

            //I AM GETTING ERROR IN THIS LINE OF CODE, THE FOR LOOP
            //It throws NullReferenceException for both this.Demands and this.Flows

            for (int i = 0; i < flows.Length; i++)
            {
                for (int j = 0; j < flows[i].Length - 1; j++)
                {
                    int dist = Math.Abs(flows[i][j + 1].Order - flows[i][j].Order);
                    float totflow = dist * demands[i];
                    total += totflow;
                }
            }
            return total;
        }
    }

    public IGene[] Genes
    {
        get
        {
            return cells;
        }
        set
        {
            cells = value;
        }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2016-04-10 08:28:27

这段代码:FacilityCell[][] fl = new FacilityCell[3][];将在构造函数中将demands设置为空,您可以在设置demands之后调用此代码。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36524265

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档