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.