title: Blazor Unhandled exception rendering component
body: <p>I'm a beginner at Blazor and am making a simple to-do list project. I got an error when adding a list.
  Unhandled exception rendering component: Object reference not set to an instance of an object.</p>
  <blockquote>
  <p>System.NullReferenceException: Object reference not set to an instance of an object.
  at FrontEnd.Pages.Index.AddList(KeyboardEventArgs e) in C:\Users\bryan\source\repos\Productivity_App\FrontEnd\Pages\Index.razor.cs:line 20
  at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)</p>
  </blockquote>
  <p>I tried searching for the problem and found a <a href="https://stackoverflow.com/q/58828878/14201840">similar post</a> but the answer didn't help me at all, looking at the question. I think it has something to do with blazor lifecycle to fix it. Here's the code related to the error.</p>
  <p><strong>Index.razor:</strong></p>
  <pre><code>&lt;div class=&quot;toDoList&quot;&gt;
      @if (ToDoListCollection != null)
      {
          @foreach (string toDoList in ToDoListCollection)
          {
              &lt;input type=&quot;checkbox&quot; id=&quot;checkbox&quot;&gt;
              &lt;label for=&quot;checkbox&quot;&gt;@toDoList&lt;/label&gt;
              &lt;hr /&gt;
          }
      }
      else {}
  &lt;/div&gt;
  </code></pre>
  <p><strong>Index.razor.cs</strong></p>
  <pre class="lang-cs prettyprint-override"><code>public partial class Index
  {
      public bool IsCompleted { get; set; }
      public string Description { get; set; }
      public List&lt;string&gt; ToDoListCollection { get; set; }
      
      public void AddList(KeyboardEventArgs e)
      {
          if (e.Key == &quot;Enter&quot; &amp;&amp; !string.IsNullOrWhiteSpace(Description) || e.Key == &quot;NumpadEnter&quot; &amp;&amp; !string.IsNullOrWhiteSpace(Description))
          {
              ToDoListCollection.Add($&quot;{Description}&quot;); // This is the line that the error message is referencing 
              Description = null;
          } 
          else {}
      }
  
  }
  </code></pre>