3

I'm writing a Xamarin.Android app and work there with a JSON API, that simply returns an empty string for the data field when it can't find the object in the database. Not a good design, but I can't change it. To check for this eventuality I wrote this short function, which I bind to the LanguageExt.Either object containing the JsonObject and which returns a left value when the data is null.

        private Either<Exception, T> CheckData<T>(T json) where T : JsonObject
        {
            if (json.Data == null)
            {
                return Left<Exception, T>(new KeyNotFoundException());
            }
            else
            {
                return Right(json);
            }
        }

Problem is, it always returns a left value. Trying to change the condition to json.Data.Equals(null) throws a NullPointerException. So far I would guess that I actually have null value in there, but when I step through the program in the debugger it's definitely filled and not null, even when I add a breakpoint right at the if line. I'm honestly at my wits end, how can an object be null, yet not null in the debugger?

Here's an image of the error: Debugger

Metalfreak
  • 149
  • 6
  • 1
    _"Object is null even though the object exists"_ -- no, that's not true. Either the reference is null and there's no object, or there's an object and the reference is not null. I wish I had a nickel for every time someone claimed that reality is different from what the debugger is telling them. When you think the debugger is lying to you, it's only because you're misinterpreting what you're seeing. Unfortunately, your question lacks any detail that would help explain how you've gone wrong. So until it's improved, the best we can do is offer the canonical "null exception" answer. – Peter Duniho Nov 26 '19 at 18:47
  • I mean, you can look at the screenshot, you can see that the debugger definitely shows that the reference is not null yet it stepped into the if clause, should I ask the question again with th calling code? – Metalfreak Nov 26 '19 at 18:51
  • just curious, could you try `if (json?.Data == null)`, and see what happens? – Matt.G Nov 26 '19 at 19:00
  • The same happens as before – Metalfreak Nov 26 '19 at 19:03
  • what about `if (json?.Data != null)` – Matt.G Nov 26 '19 at 19:05
  • Post a [mcve] that reliably reproduces the problem. The screenshot you posted doesn't show anything like what you claim. For one, there's not any exception being displayed in the screenshot. For another, even if there were, there's no proof that the exception is complaining about the thing you claim is not null. The bottom line here is: .NET doesn't throw `NullReferenceException` unless code tries to dereference a null reference, and the debugger doesn't show you non-null values for variables that are null, nor vice a versa. – Peter Duniho Nov 26 '19 at 19:08
  • Then it jumps into the else clause, I mean it kinda works, but now it will simply continue even if the data is actually null – Metalfreak Nov 26 '19 at 19:11
  • @PeterDuniho, OP's problem is that `if (json.Data == null)` is returning true, even when the Locals window clearly shows that json.Data has a non null value – Matt.G Nov 26 '19 at 19:14
  • _"Then it jumps into the else clause"_ -- no, it doesn't. Your code is _not_ executing both clauses of the `if`. Some possible explanations: You are debugging an optimized ("Release") build, which will cause imprecision in the currently executed statement the debugger shows; the `==` operator has been overloaded, and so doesn't perform the comparison you think it does; there is some concurrency involved, so the value is `null` at the test, but not later when you look at it. – Peter Duniho Nov 26 '19 at 19:15
  • @PeterDuniho, I fully agree that it's implausible that it would be true. – Matt.G Nov 26 '19 at 19:22
  • @Metalfreak, could you check the value of json.Data where the CheckData method is being called? – Matt.G Nov 26 '19 at 19:25
  • I, too, agree that it's impossible, yet it's what I'm seeing. And the value of json.Data is the same when CheckData is called and when it's in the function – Metalfreak Nov 26 '19 at 19:57
  • There could be a multi-threading / lazy loading issue. E.g. you can easily write a property that returns null on first access. that might depend on the implementation of JsonObject. What happens if you check two times / add another `if (json?.Data == null)` in line 174 and set a breakpoint there? – stb Nov 26 '19 at 21:25

1 Answers1

1

Use ==

if(json.Data == null)
{
}
LP13
  • 30,567
  • 53
  • 217
  • 400
  • I'm already doing that, it doesn't look like it in te screenshot since I'm using the Fira Code font with ligatures, but the code in there is the same as the one in the text – Metalfreak Nov 26 '19 at 18:46
  • Where is `JsonObject`? I don't think its part of `NewtonSoft`. It may be possible that `Data` property is not evaluated when you do `if(json.Data == null)`. However, when you do quick watch, visual studio will evaluate this property. Check if `data` property's `getter` has Lazy Loading. That's my guess :) – LP13 Nov 26 '19 at 21:22