title: Getting null reference exception when populating dropdown list in ASP.NET Core Razor Pages
body: <p>I am new to ASP.Net core. I am trying to populate dropdown list in class <code>Createmodel</code> from the <code>Instructor</code> model and getting a &quot;Null reference&quot; exception.</p>
  <p>This is the code in cshtml.cs file:</p>
  <pre><code>public SelectList Instructors { get; set; }
  
  public async Task OnGetAsync()
  {
      var students = from s in _context.User_Profile
                     select s;
  
      if (!string.IsNullOrEmpty(SearchString))
      {
          students = students.Where(s =&gt; s.FirstName.Contains(SearchString));
      }
  
      UserProfile = await students.ToListAsync();
  
      var instructors = from i in _context.Instructor
                        orderby i.FirstName
                        select i;
         
      Instructors = new SelectList(instructors, &quot;ID&quot;, &quot;FirstName&quot;);
  }
  </code></pre>
  <p>I have the following markup in my cshtml view:</p>
  <pre><code>&lt;div class=&quot;form-group&quot;&gt;
      &lt;label asp-for=&quot;Class_Info.Instructor&quot; class=&quot;control-label&quot;&gt;&lt;/label&gt;
      &lt;select asp-for=&quot;Class_Info.Instructor&quot; class=&quot;form-control&quot; asp-items=&quot;@Model.Instructors&quot;&gt;
           &lt;option value=&quot;&quot;&gt;-- Select --&lt;/option&gt;
      &lt;/select&gt;
  &lt;/div&gt;
  </code></pre>
  <p>I am getting the error at:</p>
  <pre><code>@Model.Instructors
  </code></pre>
  <p>I am getting this exception
  <a href="https://i.stack.imgur.com/XC7H7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/XC7H7.png" alt="This Error" /></a></p>
  <p>Please advise...</p>