Tests run from VS Code Test Explorer / altool runtests crash with NullReferenceException in NavClientHandle.Dispose after Camera.IsAvailable() (Item Card, Customer Card) – pass in AL Test Tool
1. Describe the bug
Tests that pass in the AL Test Tool (page 130451) fail when they are run with the developer tools' test execution, i.e. the VS Code Test Explorer or altool runtests (no AL test runner). The failure is a platform exception while a page is disposed:
Unexpected CLR exception thrown.: System.NullReferenceException: Object reference not set to an instance of an object. in NavClientHandle.Dispose.
It occurs as soon as Camera.IsAvailable() has been called – directly, or indirectly by opening a card page whose picture FactBox calls it in OnOpenPage ("Item Picture" on the Item Card, "Customer Picture" on the Customer Card). As a consequence, every test that opens one of these card pages as a TestPage cannot be run from the Test Explorer or with altool runtests.
2. To Reproduce
- Create an app that depends on Microsoft apps only and add the codeunit below.
- Publish it to a BC 28.5 on-premises container that contains Microsoft apps only.
- Run codeunit 50100 with
altool runtests 50100 --server https://<container> --serverinstance BC --port 7049 --tenant default --environmenttype OnPrem --authentication UserPassword --company "CRONUS International Ltd."(or from the VS Code Test Explorer). - Run the same codeunit in the AL Test Tool (page 130451) in the web client.
codeunit 50100 "Camera Dispose Repro"
{
Subtype = Test;
TestPermissions = Disabled;
[Test]
procedure "01_Control_OpenAndClosePaymentTerms"()
var
PaymentTerms: TestPage "Payment Terms";
begin
PaymentTerms.OpenView();
PaymentTerms.Close();
end;
[Test]
procedure "02_CameraIsAvailableWithoutAnyPage"()
var
Camera: Codeunit Camera;
begin
if Camera.IsAvailable() then;
end;
[Test]
procedure "03_OpenAndCloseItemCard"()
var
ItemCard: TestPage "Item Card";
begin
ItemCard.OpenView();
ItemCard.Close();
end;
[Test]
procedure "04_OpenAndCloseCustomerCard"()
var
CustomerCard: TestPage "Customer Card";
begin
CustomerCard.OpenView();
CustomerCard.Close();
end;
}
3. Expected behavior
Same result as in the AL Test Tool: all four tests pass. For example, Camera.IsAvailable() could return false (or not create the client-side object) when no client is attached, so that the page and codeunit can be disposed.
4. Actual behavior
| Test | altool runtests (no test runner) |
AL Test Tool (page 130451) |
|---|---|---|
| 01_Control_OpenAndClosePaymentTerms | pass | pass |
| 02_CameraIsAvailableWithoutAnyPage | FAIL | pass |
| 03_OpenAndCloseItemCard | FAIL | pass |
| 04_OpenAndCloseCustomerCard | FAIL | pass |
Error of 02 (03 and 04 end in the same frames, reached via the picture FactBox of the card):
Unexpected CLR exception thrown.: System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.Dynamics.Nav.Runtime.NavClientHandle.Dispose(Boolean disposing)
at Microsoft.Dynamics.Nav.Runtime.NavInteropHandleReferenceCount.Dispose()
at Microsoft.Dynamics.Nav.Runtime.NavDotNet.Clear()
at Microsoft.Dynamics.Nav.Runtime.NavDotNet.Dispose(Boolean disposing)
at Microsoft.Dynamics.Nav.Runtime.NavForm.Dispose(Boolean disposing)
at Microsoft.Dynamics.Nav.Runtime.TreeHandler.TreeObjectReferenceHandler.Dispose(Boolean disposing)
at Microsoft.Dynamics.Nav.Runtime.NavFormHandle.Dispose(Boolean disposing)
at Microsoft.Dynamics.Nav.Runtime.TreeHandler.Dispose(Boolean disposing)
at Microsoft.Dynamics.Nav.BusinessApplication.Codeunit50100._02_CameraIsAvailableWithoutAnyPage()
Analysis from the source (Base Application / System Application 28.5):
- "Item Picture"
OnOpenPage:CameraAvailable := Camera.IsAvailable();(same pattern in "Customer Picture"). - "Camera Impl."
IsAvailable()only checksGuiAllowed()– which is true in test sessions – and then calls page "Camera". - Page "Camera" holds
[RunOnClient] [WithEvents] CameraProvider: DotNet CameraProvider;.
Without an attached client there seems to be no valid client handle when this RunOnClient object is disposed. There is no way for a test to intercept before the DotNet call (OnIsCameraAvailable is raised afterwards).
5. Versions:
- AL Language: 18.0.2732683
- Visual Studio Code: 1.139.1
- Business Central: on-premises Docker container, platform 28.0.54423.0, Base Application 28.5.54151.54491 (company CRONUS International Ltd.); only Microsoft apps installed besides the repro app
- List of Visual Studio Code extensions that you have installed: not relevant for the repro above – it was run with
altool runtestsoutside VS Code. The same exception occurs for our own test codeunits in the VS Code Test Explorer. - Operating System:
- [x] Windows
- [ ] Linux
- [ ] MacOS
Final Checklist
- [x] Search the issue repository to ensure you are reporting a new issue (related, but different: #8233, #8254)
- [x] Reproduce the issue after disabling all extensions except the AL Language extension (reproduced with
altool runtests, no VS Code extensions involved) - [x] Simplify your code around the issue to better isolate the problem
Confirming we hit the same issue - identical NullReferenceException in NavClientHandle.Dispose running tests via VS Code Test Explorer, on BC platform 28.4.53241.54101 (on-premises container). A couple of additions:
TestPage "Vendor Card"crashes the same way. Confirmed via symbols that page "Vendor Picture" has the sameCamera: Codeunit Camera/CameraAvailable: Booleanpattern as "Item Picture"/"Customer Picture", so it's not just Item/Customer Card.- We tried working around it in AL with a lot of different approaches - nothing helps, the exception isn't catchable from AL code either way.
This blocks any of our tests that touch these Card pages from running in VS Code, so would appreciate a fix.
If possible, please try again on a preview build of BC 30. I believe the issue should be fixed there.
Same issue on BC 29.0 and 29.1, with four AL tool builds. In the same containers, page 130455 "Command Line Test Tool" through client services (the BcContainerHelper path) passes the same tests.
Probe (an app that depends on Microsoft apps only):
codeunit 50100 "Card Probe"
{
Subtype = Test;
TestPermissions = Disabled;
[Test]
procedure CameraIsAvailableAlone()
var
Camera: Codeunit Camera;
begin
if Camera.IsAvailable() then;
end;
[Test]
procedure CustomerCardOpenClose()
var
CustomerCard: TestPage "Customer Card";
begin
CustomerCard.OpenView();
CustomerCard.Close();
end;
// The same pattern for TestPage "Vendor Picture", "Customer Picture", "Item List" and "Aged Acc. Payable Chart".
}
Results:
| Server (platform) | Runner | Camera.IsAvailable(), Customer Card, Vendor Picture, Customer Picture |
Item List, Aged Acc. Payable Chart |
|---|---|---|---|
| BC 29.0.54011.55407 (platform 29.0.55365.0) | altool runtests 30.0.42.32495-beta, 30.0.43.45776, 30.0.44.47290-beta |
FAIL: NullReferenceException in NavClientHandle.Dispose |
pass |
| BC 29.0.54011.55407 (platform 29.0.55365.0) | page 130455 via client services | pass; Camera.IsAvailable() returns false |
pass |
| BC 29.1.57326.58090 (platform 29.0.57960.0), Microsoft's image via BcContainerHelper | altool runtests 30.0.44.47290-beta |
FAIL: same exception | pass |
| BC 29.1.57326.58090 (platform 29.0.57960.0) | page 130455 via client services | pass; Camera.IsAvailable() returns false |
pass |
A few more data points:
- Session type: both paths report
GuiAllowed() = trueandCurrentClientType() = Web.altool runtestsconnects through the test hub ("Test hub connected. TestRunnerHub connected."). - First failure: the dispose crash is not the first failure. Under
altool runteststhe server event log shows the client-side invoke failing first:
Neither event is logged when the same tests run through page 130455.Warning (NullReferenceException): DotNet invoke error. SessionID:54, Interface:, type:, methodName:IsAvailable Error (NullReferenceException): NullReferenceException occured in Dispose - The BusinessChart add-in is not the cause: the Aged Acc. Payable Chart page passes. The Vendor Card fails through its "Vendor Picture" FactBox, as @jwikman noted.
- A separate message: every test-hub run, passing ones included, also logs "Disposing Record in the wrong session, created in sessionId:N, currentThread sessionId:-1" for the Code Coverage tables 2000000288 and 2000000289. It may be unrelated.
We have no BC 30 preview environment to check the fix in.