2016-04-10 17 views
0

Tezim için Genetik Algoritma uygulaması yapmaya çalışıyorum. İki ana sınıf vardır: kromozom olarak Facility ve gen olarak FacilityCell. Ama ben Facility sınıfından fitness değeri alırken bir hata alıyorum.NullReferenceException neden özellik dizisine değer oluşturmaya çalıştığında alıyorum?

Gerekli değerler Form.cs'de ayarlanır ve algoritma çalıştırıldıktan sonra, bu özellikler Facility örneğinde null olur. Bu özellikler Facility.Flows ve Facility.Demands'dur. Nedenini anlamıyorum. Lütfen yardım et. Facility.cs den Form.cs

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; 

Code'dan

Kod kısmı:

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

Kullanılmadan önce üyeleri ilklendirmeyi düşündünüz mü? OOP'ta sınıf örneğini kullanılabilir durumda başlatan kurucular yazmayı deniyoruz. Başlatma işleminizin yarısını yapıcının dışında rastgele konumlarda gizlemek, gördüğünüz gibi kötü bir fikirdir. Bir örnek oluşturduğunuzda her zaman çalıştırılan kodun bir bit yapıcısıdır. Basit Tutun: Bunu yapıcıda başlatın ve 'yeni' operatörüne, havaya uçmayacak bir şey verdiğinden emin olabilirsiniz. –

cevap

1

Bu kodu: demands set SONRA FacilityCell[][] fl = new FacilityCell[3][]; yapıcı içinde null demands ayarlayacaktır, Sen ths kodu çağırır.

İlgili konular