This project has moved and is read-only. For the latest updates, please go here.

Object reference not set to an instance of an object

Sep 19, 2011 at 3:16 PM

Hello,

I have a problem with the creation of a word file througth a .net application.

locally on my PC all works fine, the .doc file is created and I can open it properly.

But when I publish my site to the server I encounter this error when I try to create the file:

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Exception: Object reference not set to an instance of an object.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

...

 

And this is the portion of Code where the error appear:

#region Start word and turn off msg boxes
wordApplication = new Word.Application();
wordApplication.DisplayAlerts = WdAlertLevel.wdAlertsNone;
#endregion

#region Add a new document
Word.Document newDocument = wordApplication.Documents.Add();
#endregion

#region Header of Document
foreach (Word.Section section in newDocument.Sections)
{ string picturePath = @"C:\\Temp\\logo.gif"; section.Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.InlineShapes.AddPicture(picturePath); section.Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphRight; } #endregion

The null reference exception is on the foreach loop,
I think that the variable newDocument is null.. but why ?

I'm missing something on the server ?

I will be very appreciate if someone can help me...

thanks

Sep 19, 2011 at 3:27 PM

can you clarify the exactly codeline with the exception?
i do a test with your code and works fine but one of your codelines are wrong

string picturePath = @"C:\\Temp\\logo.gif";

the @ means you dont have to use "\\". remove the @ or change "\\" to "\"


*Sebastian

 

Sep 20, 2011 at 6:46 AM

Hi Sebastian,

thank for your reply....

Locally all works fine, I don't know what is wrong...

this is the exact error:

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.


Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
   ShipmentRequest.ctrl_Invoice.ctrl_popUpInvoiceCreate_Click(Object sender, EventArgs e) in C:\ShipmentRequest\ctrl_Invoice.ascx.cs:114
   DevExpress.Web.ASPxEditors.ASPxButton.OnClick(EventArgs e) +96
   DevExpress.Web.ASPxEditors.ASPxButton.RaisePostBackEvent(String eventArgument) +539
   DevExpress.Web.ASPxClasses.ASPxWebControl.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563

 

Thank you

Sep 20, 2011 at 4:02 PM
Edited Sep 20, 2011 at 4:04 PM

i see no indicator the exception comes from netoffice in your stacktrace.'
NetOffice throws strictly COMException's.
its possible to test your netoffice code in a winforms application?
can you debug your aspx code? (i'm not an asp expert) in that case goto visual studio menu/debug/exceptions and
activate the checkbox 'Common Language Runtime Exceptions'. the debugger stops now for you on the code line with the exception.
btw: whats your word version?

section headers was related to print layout right?
i know from excel the accessing of headers and footers failed in case of the spooler service is not running.

 

 

Sep 21, 2011 at 3:40 PM

Thank for your reply....

I cann't debug the code because it is on server..

I think that the problem by executing Word on server is that the application (Word) try to start with the Internet Guest Account but cann't start...

I have playing a little bit with permissions on the COM Object permission, but now a new Exception appear...

Server Error in '/ShipmentRequest' Application.

There is insufficient memory. Save the document now.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Runtime.InteropServices.COMException: There is insufficient memory. Save the document now.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.


Stack Trace:

[COMException (0x800a13e9): There is insufficient memory. Save the document now.]

[TargetInvocationException: Exception has been thrown by the target of an invocation.]
   System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParameters) +0
   System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams) +2584
   LateBindingApi.Core.Invoker.MethodReturn(COMObject comObject, String name, Object[] paramsArray) +68

[COMException (0x80004005): See inner exception for details.]
   LateBindingApi.Core.Invoker.MethodReturn(COMObject comObject, String name, Object[] paramsArray) +105
   NetOffice.WordApi.Documents.Add() +22
   ShipmentRequest.ctrl_Invoice.ctrl_popUpInvoiceCreate_Click(Object sender, EventArgs e) in C:\ShipmentRequest\ctrl_Invoice.ascx.cs:110
   DevExpress.Web.ASPxEditors.ASPxButton.OnClick(EventArgs e) +96
   DevExpress.Web.ASPxEditors.ASPxButton.RaisePostBackEvent(String eventArgument) +539
   DevExpress.Web.ASPxClasses.ASPxWebControl.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563

In the C:\ShipmentRequest\ctrl_Invoice.ascx.cs page on line 110 i have this code:

Word.Document newDocument = wordApplication.Documents.Add();
And WordApplication is declared before like this:
wordApplication = new Word.Application();

I don't know wath to do... If someone can give me more clarifcation about this problem will be very appreciate...

Sep 21, 2011 at 4:32 PM

please read this topic completly

http://objectmix.com/cold-fusion/765392-automationexception-0x800a13e9.html

http://stackoverflow.com/questions/1491123/system-unauthorizedaccessexception-retrieving-the-com-class-factory-for-componen

Sep 22, 2011 at 8:17 AM

I have solved the issue by adding this in my web.config file:

Sep 22, 2011 at 8:18 AM

sorry for double post...

 

solved by adding this in my web.config file:

<identity impersonate="true" userName="Domain or ComputerName\UserName" password="Password"/>

Thnak you very much for your help...