There are two ways this might happen. The first is a programming error in the script such as reading or writing a cell location in an activated pivot table that is actually outside the table or table segment such as a label object or attempting an operation on a table beyond the end of the Viewer contents.
The second is a timing problem. The Viewer and the SPSS backend operate asynchronously in separate communicating processes, so, for example, you might do something in syntax to create a pivot table and then operate on that table in the Viewer process using the SpssClient apis. The backend part will complete and then send the table to the frontend (Viewer) to be rendered. However, the script code then starting might try to access or manipulate the object before rendering is complete. While the frontend should block until the object is ready, sometimes that doesn't happen as the frontend may be running several threads at the same time. This is particularly likely when the backend has generated a big block of tables or other objects that have to be rendered.
This is frustrating, because it can happen even if the user code is completely correct. What I suggest in that case is to add a very small delay (sleep) call in the script to give the Viewer time to complete the rendering of the object. Some experimentation may be required, but usually a delay of only one or two tenths of a second is sufficient.
HTH.