Null Reference Exception in BPM

When I try to save changes on order lines or create an order from a quote, I get this error pointing to a BPM:

BPM runtime caught an unexpected exception of ‘NullReferenceException’ type.
See more info in the Inner Exception section of Exception Details.

Exception caught in: Epicor.ServiceModel

Inner Exception

Object reference not set to an instance of an object.

Here’s my code:

var ttOrder = (from tt in ttOrderHed select tt).FirstOrDefault();
if (ttOrder.CustNum !=null)
{
int Cust = ttOrder.CustNum;
var ttCst = Db.Customer.Where(ttC=>ttC.CustNum == Cust).FirstOrDefault();
if (ttOrder.UserChar1 != ttCst.CustID)
{
ttOrder.UserChar1 = ttCst.CustID.ToString();
}
}

It works when modifying existing sales orders, but not with new orders it seems. Anyone see what’s wrong with it?

When does the bpm run? What type of directive is it? ttOrder or ttCst could potentially be null. If it’s running before a customer is assigned then ttCst could be giving you the error. Try adding two extra if statements one for ttOrder != null and one for ttCst != null.

1 Like

The if (ttCust != null) seems to have fixed it. I knew it would be one tiny thing I was overlooking!

1 Like