0

I am using a variable that has been initialized in my code to userInformation. The problem is when I run the program, it says that that variable is set to null.

var builder = Builders<userInformation>.Filter;
var filter = builder.Eq("VitalSigns", VitalSignsBox.Text);
userInformation findit = programController.collection.Find(filter).FirstOrDefault();
CurrentVitalSignsBox.Text = findit.VitalSigns;

By default, userInformation is a default public class. This is the variable I am referring to in that class to be passed along.

public string VitalSigns { get; set; }

Why is findit a null reference, when it is passing along information from filter?

Fildor
  • 14,510
  • 4
  • 35
  • 67
  • 1
    how should *we* know? Obviously there is no item that matches your filter. But as we don't know neither your filter nor your data, there's nothing we can do here. – MakePeaceGreatAgain Apr 12 '22 at 15:03
  • 1
    .FirstOrDefault() will get the first item in the list, but if it can't find anything at all that matches your filter, then it returns the default value (in your case, null). Essentially your list is missing the item your looking for. – DubDub Apr 12 '22 at 15:06
  • Your question is hard to read; could you edit it and put your code into a codeblock, with each expression in its own line? Either way, `findit` is null because `programController.collection.Find(filter).FirstOrDefault()` is returning null, and this is because your `filter` predicate is never returning true on any item in the collection. There's no way to help you further without additional information. – Andrio Apr 12 '22 at 15:12

0 Answers0