31 Jul 2019

Error after migrating away from ViewingApplication

After migrating away from ViewingApplication in one of the samples (required by v7), I suddenly ran into the error shown in the above picture, saying:

TypeError: Cannot read property 'width' of null

If you debug into the source code of viewer3D.js then you'll see that inside Viewer3D.prototype.initialize, the function this.getDimensions() returned null since inside it this.container is undefined - but why is that?

If I check the HTML page I can see that the <div> for the Viewer is placed at the end, after the <script> section, and in case of this sample the function creating the Viewer gets called straight away instead of waiting for the document to be fully loaded - e.g. using $.ready()

So at the time of the creation of the Viewer that <div> is still not available - which means that I'm basically passing in undefined to the Autodesk.Viewing.GuiViewer3D() constructor.

The simplest solution is to just move that <div> before the <script> section.

The placement of the Viewer <div> at the end of the HTML page did not cause an issue when using ViewingApplication, as that only instantiated the Viewer (and was looking for the Viewer <div>) when you actually started loading a specific document. And by that time, the <div> was available.

FYI: it was not straight forward to see this error as I was working in a CefSharp browser. Once I realized that I can bring up the Debug Console using ShowDevTools(), it was much easier to find ?

ChromiumWebBrowser browser = new ChromiumWebBrowser();
browser.ShowDevTools();

 

Related Article