1 Reply Latest reply on Aug 15, 2011 10:32 AM by John Hawkinson

    exception Object reference not set to an instance of an object sometimes

    xmlapi Level 1

      Sometimes, I get the following exception (most of the time, it doesn't happen).

              for (int j = 1; j <= courseDocumentsCount; j++)

       

                          {

                              dynamic doc = app.Documents[1];

       

                              doc.Save(Utility.MergedFolder + "\\courses_" + j + ".indd");

                              doc.Close(idSaveOptions.idNo);

       

                          }

      The exception is "Object reference not set to a instance of an object". It happens when I call doc.Save() and it happens because doc variable appears to be null.

       

      Could this be some kind of "delay" in com intereop? In the previous iteration for the for loop, a document is closed and while Indesign is "in process of closing" the first document (the documents are large)....the code moves to the next iteration BUT opens the same index (since the original document is still closing, indesign returns the same reference). By the time it gets to the doc.Save() line, the original document has closed and the reference is now null. Could this be the issue?

       

      This is Indesign Server btw. If this is the issue, how do I solve it? I noticed there is an app.WaitForAllTasks() but I don't think that's available in Server.