Announcements
Hi
I have (In Dynamics 365 FO) made a class as a DataEventHandler for Updated on the table CustTable.
When I want to compare a field, to see is the data has been changed, I call CustTable.Orig().
This works just fine, if there is a change. But if there is no change upon clicking "Update", there is no orig, and I get this error: "Object reference not set to an instance of an object."
I would like to check whether to orig is null or not, befor using it, so I don't get the error.
How do I code (X++) a check for whether the Orig is null ?
Best Regards
Peter
Hi again
I have now tried, and you are right. Orig is not null :-)
Teh error comes when I compare CustTable.PaymTermId with origCustTable.PaymTermId.
The error must be that the Orig is not instanciated when there is no change.
I will have to dig deeper into this.
Thanks
Unfortunately it didn't answer my question. Please walk through the following piece of code in debugger and tell me at which line it fails.
CustTable origCustTable = custTable.orig(); if (origCustTable) {} if (custTable.orig()) {}
You can see exception details (including the stack trace) in the debugger, at Locals:
I will try to be more precise.
Orig is not null if an update is performed with change.
Orig is null if an update is performed without change.
If in the code I write "if (CustTable.Orig()), the error is thrown in D365FO. I see no stack trace. The error only comes if the orig is null. If there is a field in CustTable that has been changed, the orig is not null, and all is good.
I hope this is more clear.
It doesn't make sense to me. You're saying that CustTable.orig() returns null, i.e. it doesn't throw any exception. And then you're saying that if (CustTable.orig()) fails, but because CustTable.orig() returns null, it's effective the same as if (null). I don't see where is could fail.
Can you please explicitly confirm that CustTable.orig() works and if (CustTable.orig()) throws "Object reference not set to an instance of an object"?
What is the stack trace of the exception?
P.S. It does not throw an error when building the code. The error comes upon executing from D365FO
Hi Martin
Thanks for your fast respons.
orig() returns null, and if I use "if (CustTable.Orig())" or something like that, I get the error.
Therefore I need to check whether orig is null or not.
I have tried "if (CustTable.Orig())" and "if (CustTable.Orig() != null)". Both throws the error.
Are you saying that orig() throws an exception, or that orig() returns null and you get an exception when you're trying to access fields values of this null reference? If it's the latter, simply use something like if (origCustTable).
Nevertheless it sounds a bit strange to me. The fact that you didn't change anything doesn't mean that there was no pre-update version of the record, which is what orig() returns. What if you use CoC on CustTable.update() and get orig() before calling next? Will it behave correctly?
André Arnaud de Cal...
294,095
Super User 2025 Season 1
Martin Dráb
232,866
Most Valuable Professional
nmaenpaa
101,158
Moderator