0

I have this code

var vehicle = await _vehicleService.GetVehicle(registrationNumber);

if (vehicle == null) 
  return false;

We use Sentry error logging and for some reason, we get a steady flow of errors reported at the line and char of the return false text (line 28 char 34 in our case). How is that even possible? This whole line is to handle the case where we don't get any vehicle back from the service and therefor returns false to the calling method. enter image description here

Method that causes the error:

public async Task<bool> CreateCallback(string registrationNumber, int departmentNr, string departmentName, string customerName, string customerPhone, int multiBrandErrand, int bmwBrandErrand)
{
    var vehicle = await _vehicleService.GetVehicle(registrationNumber);

    if (vehicle == null) return false;

    int errand;
    string entrance;

    if (vehicle.Brand.ToLower() == "bmw" || vehicle.Brand.ToLower() == "mini")

The called method, using flurl:

public async Task<AutonetVehicleResult> GetVehicle(string registrationNumber)
{
    try
    {
      var vehicle = await $"https://fakeapiurl/api/vehicle/{registrationNumber}"
                .GetJsonAsync<AutonetVehicleResult>();
      return vehicle;
    }
    catch (Exception)
    {
      return null;
    }
}
Hyzac
  • 199
  • 1
  • 15
  • Can you show all code in question? What else are you returning when it's not `null`? It's evident you're using something that isn't there, `null` and this code alone will not help us help you. – zaggler Mar 02 '20 at 15:49
  • can you specify the error message? – Falco Alexander Mar 02 '20 at 15:49
  • 1
    Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Selim Yildiz Mar 02 '20 at 15:51
  • 3
    It isn't possible. Line numbers are not always 100% reliable, depending on optimization settings and how up-to-date the symbols are. Check for other, more plausible candidates, like `_vehicleService` being `null`, or the lines after that containing something suspect. – Jeroen Mostert Mar 02 '20 at 15:52
  • Make sure you're not running in the Release configuration first. That will definitely throw off line numbers and make error locations seem goofy. – Broots Waymb Mar 02 '20 at 15:53
  • I'm guessing that the null reference exception is actually occurring in _vehicleService.GetVehicle(registrationNumber). It may just be throwing on the next line down because of oddities in handing the await and when the value of vehicle is resolved. It's hard to say without fully functioning code. – TJ Rockefeller Mar 02 '20 at 15:54
  • The symbols are always updated with the source code so the pdb-file is the same version as the current dll – Hyzac Mar 02 '20 at 15:54
  • I'll the third the rest of the team here - *show all of the pertinent code* - and obfuscate anything not necessary to see. – gravity Mar 02 '20 at 15:54
  • All used code included up to the point the error is reported – Hyzac Mar 02 '20 at 15:59
  • https://dotnetfiddle.net/PtXSBV. Fiddle to show how a null check can result in a null reference exception. Not sure if it would result in your specific null reference exception though. – Knoop Mar 02 '20 at 16:08
  • Does sentry modify the IL ? If yes, I bet for a bug in sentry management of async/await calls. – Orace Mar 02 '20 at 16:08
  • And `_vehicleService` is definitely not null? – Matthew Watson Mar 02 '20 at 16:09
  • _vehicleService is added through DI with a IVehicleService interface and only contains this one method. So if nothings is really wrong with the initialization of the site the _vehicleService should not be null. DIResolver initialization: expression.For().Use(); This service is used quite a lot from 5 references but only this call to it gives error. – Hyzac Mar 02 '20 at 16:15
  • 1
    @JeroenMostert Isn't it possible if someone overwrites the `==` operator for that class in a bad way? – Knoop Mar 02 '20 at 16:20
  • Included the continuing code and reading from some of the comments the error could be in the following vehicle.brand.ToLower() even if thats 5 rows after the reported error line? – Hyzac Mar 02 '20 at 16:43
  • Maybe the `GetJsonAsync` is returning null? Try changing the api call to sync and see if it's the same line – lukaszberwid Mar 02 '20 at 16:58
  • @Knoop: yes. When I said "impossible", I only meant "for sufficiently small values of 'impossible'". When every reasonable avenue has been exhausted you may as well check for overloaded assignment operators. (This possibility could be excluded by rewriting it as `Object.ReferenceEquals(vehicle, null)`). – Jeroen Mostert Mar 02 '20 at 19:03

0 Answers0