title: Error System.NullReferenceException while assigning List<T> to a property C#
body: <p>This is not something I don't know what is <code>NullRefrenceException</code>. There is some uncertain situation where I was getting the error.</p>
  
  <p>I'm trying to assign values to properties of some classes.</p>
  
  <p>Here are my classes.</p>
  
  <pre><code>// These classes are in EmailProcess namspace
  public class ActionedEmailReport
  {
      public Message Message { get; set; }
      public string SaveToSentItems { get; set; }
  }
  
  public class ToRecipient
  {
      public EmailObject.EmailAddress EmailAddress { get; set; }
  }
  
  public class Message
  {
      public string Subject { get; set; }
      public Body Body { get; set; }
      public List&lt;ToRecipient&gt; ToRecipients { get; set; }
  }
  
  public class Body
  {
      public string ContentType { get; set; }
      public string Content { get; set; }
  }
  
  // Below class in EmailObject namespace.
  namespace EmailObject
  {
     public class EmailAddress
     {
        public string Address { get; set; }
     }
  }
  </code></pre>
  
  <p>Here is my code to assign values to the properties of the classes.</p>
  
  <pre><code>public void EmailProcessing(string recepeint)
  {
     ActionedEmailReport actionedReport = new ActionedEmailReport();
     List&lt;ToRecipient&gt;toRecipient = new List&lt;ToRecipient&gt;();
     EmailObject.EmailAddress emailAddress= new EmailObject.EmailAddress();
     emailAddress.Address = recepeint;
     toRecipient.Add(new ToRecipient()
  {
     EmailAddress=emailAddress
  });
  
     // I'm getting error on the below line.    
     actionedReport.Message.ToRecipients = toRecipient;
     actionedReport.Message.Body.Content = "Hello";
     actionedReport.Message.Body.ContentType = "Text";
     actionedReport.SaveToSentItems = "True";
     actionedReport.Message.Subject = "Demo Email"
  }
  </code></pre>
  
  <p>On the line <code>actionedReport.Message.ToRecipients = toRec;</code> I am getting error that </p>
  
  <blockquote>
    <p>System.NullReferenceException:Object reference not set to an instance of an object.</p>
  </blockquote>
  
  <p>I have checked inputs properly and sure that I am assigning some value to <code>toRecipient</code> then why I am getting this error. This is driving me crazy.</p>