Ainars Osmanis

Ainars Osmanis

  • NA
  • 32
  • 490

Cannot set data in datamodel

Aug 16 2022 8:15 AM

My sample use Newtonsoft and in .NET framework 4.8.
There 2 dataModels and simple code from my project:


       public class DDZv4Model
        {         
            public bool? Controle { get; set; }
            public int Year { get; set; }
            public System.Collections.Generic.ICollection<R15> Table1 { get; set; }
        }

       public class R15
        {          
            public string ID { get; set; }
            public string Texts { get; set; }
        }

public void running()
{
       DDZv4DataModels.DDZv4Model DDZv4dati = New DDZv4DataModels.DDZv4Model;        
       DDZv4DataModels.R15 Tabs = New DDZv4DataModels.R15;

        DDZv4dati.Controle=True;
        DDZv4dati.Year =Now.year;
        DDZv4dati.Year =now.year;

        Tabs.ID = 1;
        Tabs.Texts = "Text text 1";
        DDZv4dati.Table1.Add(Tabs);
        Tabs.ID = 2;
        Tabs.Texts = "Text text 2";
        DDZv4dati.Table1.Add(Tabs);
}

On lines (DDZv4dati.Table1.Add(Tabs)) in runtime generate this error: 
System.NullReferenceException: 'Object reference not set to an instance of an object.'
Why cannot set data to Table1?


Answers (4)