title: NullReferenceException when trying to access to foreign key of parent table
body: <p>I have been learning C# and ASP.NET Core for around 1 month. I have a problem with this NullRefrences error and I can't find out the reason. I create a news webpage by asp.net core and in the administration section when I create a comment section for the article I get the "NullReference" error to show comments belong to which article. please help me out on this one.
  Article Model:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel.DataAnnotations;
  
  namespace WebNews.Models
  {
      public class Article
      {
          public int Id { get; set; }
          public string Title { get; set; }
          public string Body { get; set; }
          [DataType(DataType.DateTime)]
          public DateTime PublishedDate { get; set; }
          public string Writer { get; set; }
          public string Photo { get; set; }
          public bool IsPublished { get; set; }
          [DataType(DataType.DateTime)]
          public DateTime LastModified { get; set; }
          public string Categories { get; set; }
          public virtual ICollection&lt;Comment&gt; Comments { get; set; }
  
     }
  }
  </code></pre>
  
  <p>this is my comment Model:</p>
  
  <pre><code>using System;
  using System.ComponentModel.DataAnnotations;
  
  namespace WebNews.Models
  {
      public class Comment
      {
  
      public int Id { get; set; }
      [Required]
      public string Name { get; set; }
      [Required]
      public string Body { get; set; }
      [DataType(DataType.DateTime)]
      public DateTime CommentDate { get; set; }
      public virtual Article Article { get; set; }
      public bool IsProved { get; set; }
      }
  }
  </code></pre>
  
  <p>This is my Controller:</p>
  
  <pre><code>    [HttpGet]
      public IActionResult ListComments()
      {
          var comments = _comment.AllComments();
          AdministrationListCommentViewModel model = new AdministrationListCommentViewModel();
          foreach (var comment in comments)
          {
              AdministrationListCommentsViewModel modell = new AdministrationListCommentsViewModel
              {
                  ArticleTitle = article.GetArticle(comment.Article.Id).Title,
                  Body = comment.Body,
                  Id = comment.Id,
                  IsProved = comment.IsProved,
                  PublishedDate = comment.CommentDate,
                  Title = comment.Name,
              };
  
              model.Comments.Add(modell);
          }
          return View(model);
      }
  </code></pre>
  
  <p>this my view models: both of them are here:</p>
  
  <pre><code> using System;
   using System.ComponentModel.DataAnnotations;
   using WebNews.Models;
  
   namespace WebNews.ViewModels
   {
       public class AdministrationListCommentsViewModel
       {
      public int Id { get; set; }
      public string Title { get; set; }
      public string Body { get; set; }
      public Article Article { get; set; }
      public bool IsProved { get; set; }
      [DataType(DataType.DateTime)]
      public DateTime PublishedDate { get; set; }
      public string ArticleTitle { get; set; }
  
      }
  }
  
  
  using System;
  using System.Collections.Generic;
  using System.ComponentModel.DataAnnotations;
  using System.Linq;
  using System.Threading.Tasks;
  using WebNews.Models;
  
  namespace WebNews.ViewModels
  {
      public class AdministrationListCommentViewModel
      {
          public List&lt;AdministrationListCommentsViewModel&gt; Comments { get; set; }
      }  
  }
  </code></pre>
  
  <p>and this is my view:</p>
  
  <pre><code>    &lt;div class="card"&gt;
      &lt;div class="card-header"&gt;
          &lt;h5 style="display: inline-block"&gt;Article`s List&lt;/h5&gt;
          &lt;button style="display: inline-block; position:fixed; left: 1600px; top: 18px; z-index: 9999; background-color:#22119d; " type="submit" class="btn btn-primary m-md-n1"&gt;Update&lt;/button&gt;
      &lt;/div&gt;
      &lt;table class="table" style="width: 1570px;"&gt;
          &lt;thead&gt;
              &lt;tr&gt;
                  &lt;th scope="col"&gt;#&lt;/th&gt;
                  &lt;th scope="col"&gt;Author&lt;/th&gt;
                  &lt;th scope="col"&gt;Edit&lt;/th&gt;
                  &lt;th scope="col"&gt;Article&lt;/th&gt;
                  &lt;th scope="col"&gt;Is Published&lt;/th&gt;
              &lt;/tr&gt;
          &lt;/thead&gt;
          @for (int i = 0; i &lt; Model.Comments.Count; i++)
          {
              &lt;tbody&gt;
                  &lt;tr&gt;
                      &lt;th scope="row"&gt;@number &lt;/th&gt;
                      &lt;td&gt;@Model.Comments[i].Title&lt;/td&gt;
                      &lt;td&gt;
                          &lt;a href="#"&gt;Edit&lt;/a&gt;
                      &lt;/td&gt;
                      &lt;th scope="row"&gt;@Model.Comments[i].ArticleTitle&lt;/th&gt;
                      &lt;td&gt;
                          &lt;div class="form-group m-1"&gt;
                              &lt;input class="form-check-input" style="position: relative; left: 50px; top: -3px;" type="checkbox" value="@Model.Comments[i].IsProved"&gt;
                              &lt;label class="form-check-input" asp-for="@Model.Comments[i].IsProved" hidden&gt;&lt;/label&gt;
                          &lt;/div&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
              &lt;/tbody&gt;
              @(number = number + 1);
  
          }
  
      &lt;/table&gt;
      &lt;/div&gt;
  </code></pre>
  
  <p>I don't even know is this how you make comment for the article or not or is there any better way. 
  I have used view model in the controller because I wanted to be able to edit isproved of my comment on the comment list page.
  when I run these codes I get the "NullReference" error for that article title.</p>