Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method returning a non-null value still assigns null to a variable

Tags:

c#

ef-core-2.2

I have a method that searches something in my database according to a field. when calling that method and saving the returning value in a variable, the returning value is not null at the end of the function. After the function has been called and I debug what is available in my variable, the value is still null!

In one class I have:

var box = _someClass.GetBoxByRfidAsync("testvalue");
public Box GetBoxByRfidAsync(string rfid)
{
    var foundBox = _dc.Boxes
        .Include(b => b.Stack)
        .Include(b => b.Type)
        .Where(box => box.RFID1 == rfid)
        .FirstOrDefault();

     return foundBox;
}

The box class looks like this:

public class Box
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }
    public string RFID1 { get; set; }
    public string Rit { get; set; }
    public Stack Stack { get; set; }
    public int PosInStack { get; set; } //lowest box is 0, highest is 7
    public BoxType Type { get; set; }
    public DateTime Inserted { get; set; }

}

The method gets executed and at the end of GetBoxByRfidAsync I can see using the debugger that foundBox is NOT null.

I step over the end of the function and go to the assignment of the foundBox value to the box variable. For some reason the 'box' variable stays null and does not change.

The box variable should be equal to the foundBox value, but it stays null.

Here are some screenshots of my debugging session:

End of method Immediate window showing box is still null

like image 872
Bastien verschaete Avatar asked Mar 31 '26 22:03

Bastien verschaete


1 Answers

The error you show in the debugger doesn't mean that box is null. If box was null, it would literally just report: null.

Instead, the box is non-null, and something that the debugger is evaluating to display the value is throwing an exception. Perhaps a broken ToString(). The fact that it mentions ListDictionaryInternal means that you might want to look in particular at any collection or dictionary usage inside the type, and perhaps anything that is marked with "debugger" attributes such as [DebuggerDisplay(...)].

If you want to test whether box is null, ask the immediate console:

box == null

and it will return true or false (caveat: assuming that there isn't a broken static equality operator, which is also possible; to avoid that, a good alternative is:

box is object

which will reliably return true if it is non-null, and false if it is null, without using static equality operators.


Edit: based on the discussion in the comments, I'm overwhelmingly convinced that this is a broken == operator, as demonstrated here:

enter image description here

like image 177
Marc Gravell Avatar answered Apr 02 '26 13:04

Marc Gravell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!