I have an Epicor BPM in which I'm trying to test if an order (IE an entry in a temporary table ttOrderHed) has any lines in it (IE entries in a database table Db.OrderDtl).
When I test with an order that is known to have lines, this works; and when I use an order that doesn't, it returns a NullReferenceException:
var oh = (from o in ttOrderHed
select o).FirstOrDefault();
var od = (from d in Db.OrderDtl
where d.Company == oh.Company &&
d.OrderNum == oh.OrderNum
select d).FirstOrDefault();
if ( od != null)
{
//do some Things and Stuff
}
I thought maybe I was testing something null and therefore dropping out. I tried to get around it like this:
if (!(od == null)
{
//do stuff
}
And got the same exception.
I have got debugging variables sprinkled throughout and I can say for sure that any attempt to do anything with var od
makes me drop out of warp. For example, if I try to assign a value from it to a debugging variable that is already declared elsewhere b1 = od.OrderNum.ToString();
I get a NullReferenceException here too, just earlier.
I've spent the night researching this and have read this very good overview here:
What is a NullReferenceException, and how do I fix it?
and so far I'm drawing a blank. Part of the issue, I suppose, is I'm working inside Epicor's buil-in IDE which has a very limited syntax checker (it says I'm good to go) and an ersatz "Intellisense" tool, but otherwise provides no feedback or error messages. I can send output to the server's event viewer, of course, but all I get is the NullReferenceException itself.
Any feedback or suggestions would be appreciated. Many thanks in advance.
EDIT: in case it helps anyone, here's the code that worked in the end. I still have a lot to do but this one is solved.
if (ttOrderHed.Any())
{
var oh = (from o in ttOrderHed select o).FirstOrDefault();
if ((from d in Db.OrderDtl where d.OrderNum == oh.OrderNum select d).Any())
{
var od = (from d in Db.OrderDtl
where d.OrderNum == oh.OrderNum
select d).FirstOrDefault();
var tmp = od.OrderNum;
// do stuff
} // end if OrderDtl
} // end if ttOrderHed
It seems that you are new to linq and design patterns. It is usually a better idea to check if a collection is not empty before querying it. With that in mind, the following should work: