0

So I was debugging some code and I've came across a weird issue which I think should not happen, here is the code of reference

 if (result.success) 
 {
    List<int> productids = addOrder.items.Select(x=>x.product_id).ToList();
    //More code
 }

Now when I put the debugger inside the if block and execute the code it throws the following error object reference is.... Here is another snippet

 if (result.success) 
 {
    var a = 10;
    var b = a + 10;
    var c = a + b;
    var foo = new orders();
    List<int> productids = addOrder.items.Select(x=>x.product_id).ToList();
    //More code
 }

if the debugger execute the initialization for a, b, c and foo it works as expected but when it reaches for the initialization of productids it throws the error but the addOrder.items is not null as well as the product_id property because these are the values directly provided on the request and are being deserialized correctly here. Also adding a third snippet

 if (true) 
 {
    var a = 10;
    var b = a + 10;
    var c = a + b;
    var foo = new orders();
    List<int> productids = addOrder.items.Select(x=>x.product_id).ToList();
    //More code
 }

Here the code executes smoothly without any hiccups. I'm just confused why is this happening because executing any line during debugging should throw an error if the code isn't directly depended upon a variable which isn't initialized. Attaching the whole code as a reference if there is something which I'm missing

Foo result = null;
try{
   //More code
   result = await GetResult();
   //More code

   if(result.success){
        List<int> productids = addOrder.items.Select(x=>x.product_id).ToList();
       //More code
   }
}
catch(Exception ex){
   /* 
   Some Code
   .
   .
   .
   */
}

This the how the whole function is working.

  • Is any item within your `addOrder.items` null? – Marvin Klein Jun 13 '23 at 09:19
  • "because these are the values directly provided on the request and are being deserialized correctly here" did you varify that assumption in your debugger? Did you check these values *not* to be null, or do you just *assume* so? In that statement there's nothing despite these two variables, so one of them *must* be `null`. – MakePeaceGreatAgain Jun 13 '23 at 09:22
  • The problem is connected to addOrder. But your code shows nothing about that. What is it how its filled with what is it filled? – Ralf Jun 13 '23 at 09:24
  • The whole json is filled with items being an array and product_id as integer, the items is not null and I've verified it when debugging and have been stuck on this problem for couple of days now @MakePeaceGreatAgain – Muhammad Jamali Jun 13 '23 at 09:30
  • The values are being received at the controller level and this code is from a webapi, the json received get deserialized to an instance of addorder @Ralf – Muhammad Jamali Jun 13 '23 at 09:32
  • If I run the addOrder.items.Select(x=>x.product_id).ToList() in the immediate window of the visual studio it works but inside the if statement it doesn't during the debug mode. – Muhammad Jamali Jun 13 '23 at 09:35
  • Do you reach your catch block at all? Or are you fumbled by a silent exception because your debugging setting is "catch every exception" but actually does not matter? – Ralf Jun 13 '23 at 09:41
  • Yes the exception reaches the catch block and it gets thrown at the exact line of addOrder.items and on the lines down below it where linq is being used. – Muhammad Jamali Jun 13 '23 at 09:48

0 Answers0