none
Null pointer exception comes when loading the word document RRS feed

  • Question

  • Hi,

    I have a word embedded windows application developed in c#. when I am initially loading the word, time to time a null pointer exception comes. following is the line of code I am using for loading the word.

    wd = new Word.ApplicationClass();

    document = wd.Documents.Add(ref tempFilePath, ref nullobj, ref nullobj, ref nullobj);

     

    seems like when the second line runs, still the wd object is null.

    what can be the reason for this? this is not a regular error and comes time to time. Thanks in Advance.


    -Madura Shantha- Code as if the next guy to maintain your code is a homicidal maniac who knows where you live.
    Thursday, May 5, 2011 5:45 PM

Answers

  • Hi Madura,

    Just a precaution, when you setup the loop and assuming you put in a 1 second delay counter, also add a fail safe counter that doesn't allow your code to go into an infinite loop. When I've used these type of delay/check loops with my delay set to 1 second, my fail safe counter is set to loop no more than 9 times. If the fail safe gets to > 9 then I display a message and end the routine.


    Regards, Rich
    Friday, May 6, 2011 4:14 AM

All replies

  • Hi Madura,

    I might guess it to be a timing issue. If a version of Word is already open then everything works fine, but if it has to load a fresh copy from scratch then it takes longer. I suppose this could happen if for some reason the loading of Word is taking exceptionally long due to add-ins. What version of Word are you running?

    You could put some error exception handling around the code and if the null condition occurs loop for a period of time to wait/check if the condition clears.

    Hope this helps


    Regards, Rich
    Thursday, May 5, 2011 6:57 PM
  • Thanks a lot Rich,

    I am using word 2007.I think having exception handling code inside a loop will work.


    -Madura Shantha- Code as if the next guy to maintain your code is a homicidal maniac who knows where you live.
    Friday, May 6, 2011 3:46 AM
  • Hi Madura,

    Just a precaution, when you setup the loop and assuming you put in a 1 second delay counter, also add a fail safe counter that doesn't allow your code to go into an infinite loop. When I've used these type of delay/check loops with my delay set to 1 second, my fail safe counter is set to loop no more than 9 times. If the fail safe gets to > 9 then I display a message and end the routine.


    Regards, Rich
    Friday, May 6, 2011 4:14 AM
  • Another problem.

    time to time, the word control component(user control) is getting null. (the component in the window interface). the component is not loaded on the interface and gives a null pointer exception in the load event of the page. what can be the reason? please help.


    -Madura Shantha- Code as if the next guy to maintain your code is a homicidal maniac who knows where you live.
    Friday, May 6, 2011 6:09 AM