title: Moq setup gives error System.NullReferenceException
body: <p>I am writing Unit tests for controller class using XUNIT. Here is my code.</p>
  
  <pre><code>private Mock&lt;StudentService&gt; _StudentService;
  private StudentController StudentController;
  
  [Fact]
  public void GetStudentsBySubject_ShoutReturnNotNull()
  {
      List&lt;StudentViewModel&gt; Students = new List&lt;StudentViewModel&gt;();
   _studentService = new Mock&lt;StudentService&gt;();
      string Subject = "Math";
      _StudentService.Setup(x =&gt; x.GetStudentsBySubject(Subject))
        .Returns((Students));
  
      var controller = new StudentController(_StudentService.Object);
  
      var result = controller.GetStudentsBySubject(Subject);
  
      Assert.IsType&lt;List&lt;StudentViewModel&gt;&gt;(result);            
  }
  </code></pre>
  
  <p>It throws error at line.</p>
  
  <pre><code>_StudentService.Setup(x =&gt; x.GetCheapestStudentsBySubject(Subject))
        .Returns((Students));
  
  </code></pre>
  
  <blockquote>
    <p>Message: System.NullReferenceException : Object reference not set to an instance of an object</p>
  </blockquote>
  
  <p>I tried many changes. But nothing worked. What could I be doing wrong here.</p>
  
  <p>The following error appears after fixing the nullreference error:</p>
  
  <blockquote>
    <p>System.NotSupportedException : Unsupported expression: x => x.GetStudentsBySubject(StudentControllerTests.&lt;>c__DisplayClass2_0.Subject) Non-overridable members (here: StudentService.GetStudentsBySubject) may not be used in setup / verification expressions</p>
  </blockquote>