AX7: Chain of command: “Reference is not set to an instance of an object”
Have you ever tried to extend an object with the Chain of Command feature introduced with PU 9 and got the “Reference is not set to an instance of an object.” error?
TLDR: It’s a bug that is addressed in PU 13. The cause is that there exists a form that is named as your object which you try to extend with chain of command.
Test setup
To reproduce the error I simply created an extension class for the PurchTable table. I extended the initValue method by just adding an info call, where I wanted to get the PurchId form this.
[ExtensionOf(tableStr(PurchTable))] | |
final class CoCErr_Extension | |
{ | |
void initValue(PurchaseType _purchaseType = PurchParameters::find().PurchaseType) | |
{ | |
info(strFmt("My Extension: %1", this.PurchId)); | |
next initValue(_purchaseType); | |
} | |
} |
The error
When you now go on and try to create a new purchase order you will get the following error:
If you attached the debugger, you will see where it throws the exception.
As you can see in the call stack, this is indeed null.
Cause of the reference error
First of all there is not necessarily something wrong with your code. If you hit the link from the intro of this article, you will see the following post in the comment section:
So the “Reference is not set to an instance of an object.” error is always thrown when we try to make use of chain of command, where a form exists with the same name as the table you want to extend and you’re on an older version than PU 13.
In our example we used the PurchTable table and there also exists a PurchTable form. The test setup is also < PU 13.
As a workaround you can try to make use of a pre or post event handler and add your extension on that way.