"Object reference not set to an instance of an object"

I received the above error while attempting to execute the following statement:

doc.MailMerge.Execute(fieldNames, fieldValues);

I have, I believe initialized all values and defined them as required. I am attaching the stack trace, code snipet and template.

I was essentially running this code under a release 8 assembly and I am in the process of updated to release 10.

Hello

Thanks for your request. I cannot reproduce the problem on my side using the latest version of Aspose.Words (10.1.0) and the following code for testing:

Document doc = new Document("C:\\Temp\\Schedule2.docx");
String[] fieldNames = new String[] { "CurrentDate", "CurrTime", "DoctorName", "StartDate", "EndDate", "StartDay", "EndDay", "CenterName" };
Object[] fieldValues = new Object[] { DateTime.Now, DateTime.Now, "Andrey", DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now, "Andrey" };
doc.MailMerge.Execute(fieldNames, fieldValues);
// Save output document.
doc.Save("C:\\Temp\\out.docx");

You can download the latest version from here:

https://downloads.aspose.com/words/net

Best regards,

I am running 10.1.0 runtime version v2.0.50727.

I executed the following:

string reportDir = HttpContext.Current.Server.MapPath("Reports/");
string filePath = Path.Combine(reportDir, template);
if (!File.Exists(filePath))
return string.Format("Report {0} template does not exist on server", template);
if (reporttype == "I") 
PopulateIncident();
else
PopulateEvent();
PopulateMisc();
Document doc = new Document(filePath);
// Header Merge Fields
String[] fieldNames = new String[] 
{"CurrentDate","CurrTime","DoctorName","StartDate","EndDate","StartDay","EndDay","CenterName"};
Object[] fieldValues = new Object[]
// {CurrentDate,CurrTime,DoctorName,StartDate,EndDate,StartDay,EndDay,CenterName};
{ DateTime.Now, DateTime.Now, "Keith", DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now, "Keith" }; 
doc.MailMerge.Execute(fieldNames, fieldValues);
return "doc.MailMerge.Execute sucessful";

I received the identical object reference error. I am running this solution on a hosted Network Solutions site, thus I have to use the remote filePath. The trace does indicate it is within an Aspose assembly. The return statement following the doc.MailMerge.Execute(…) call is never executed. In a prior execution I included a return prior to the doc.MailMergeExecute() call, it executed normally and returned me to the application.

I rechecked the Aspose.Words.dll assembly loaded on the Website, it is the 10.1.0 version (Downloaded 6/13/2011).

Any other suggestions on where I should look?

Hello

Thanks for your inquiry. Unfortunately, I cannot reproduce the problem on my side. Could you please create simple application, which will demonstrate the problem on my side? I will check it and provide you more information.

Best regards,

Hi Keith,

Thank you for additional information. I run your document on my side and it works fine. No exceptions are thrown. The output document is properly filled with data.

Please make sure that you referenced the correct dll in your application, i.e. the latest version of Aspose.Words. Also, have you tried to run this code in a standalone application? Does it work in this case? If it does not, please attach this simple application here, this will help us to reproduce the problem.

One more thing to check, when Aspose.Words executes mail merge, it also updates fields in the document. Updating fields requires updating page layout, and Aspose.Words needs fonts to build page layout. So, if Aspose.Words cannot access any installed fonts, it can throw an exception. Just for testing, try to create a folder that Aspose.Words can access (in root of your web app), put few TTF fonts in this folder and set it as fonts directory as described here:
https://reference.aspose.com/words/net/aspose.words.fonts/fontsettings/

Best regards,

Alexy;

Thanks for all the help! I am not sure if I am invoking the Fonts methods correctly.

I basically added the two lines:

string fontDirtory = HttpCopntext.Current.Server.MapPath("../Fonts");
FontSetings.SetFontsFolder(fontDirectory, false);

I then receive :

Exception Details: System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
Source Error:

[No relevant source lines]
Source File: App_Web_g4uouhgy.0.cs Line: 0

Stack Trace:

[SecurityException: Request for the permission of type ‘System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089’ failed.]
System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet) +0
System.Security.CodeAccessPermission.Demand() +58
System.Environment.GetEnvironmentVariable(String variable) +66
xf9a9481c3f63a419.xed747ca236d86aa0.x0f5d18019ab06bc5() +13
xf9a9481c3f63a419.xed747ca236d86aa0.x3eaacfcf484aabf1() +26
xc23bef7190ca8f55.x6412d0c71c34c05c.x99a36cf35453249e(String x4fd966aaf32161e8, xd345c73dd1b16b74 x7d95d971d8923f4c) +65
xc23bef7190ca8f55.x6412d0c71c34c05c.x99a36cf35453249e(String[] x7e2cbd5b7f6c8468) +48
xc23bef7190ca8f55.x6412d0c71c34c05c.get_x99f0e2a5ac5ce2db() +70
xc23bef7190ca8f55.x6412d0c71c34c05c.xd3f8e541b341f67a() +11
xc23bef7190ca8f55.x6412d0c71c34c05c.x9059a3203c8fc855(String xa79a9f649c74f4a4, FontStyle x44ecfea61c937b8e, String x050683ee5a5f962b, Boolean x41c20a2d94f1f466) +316
x59d6a4fc5007b7a4.xacc55eb1e4595209.xc06de25aa714683f(x1d1dd20018fcde10 x26094932cf7a9139, x000f21cbda739311 x6f02b6a80bf6b36f) +57
x59d6a4fc5007b7a4.xbfac429e165a6a0d.xee5277c7de5be8e4(Char x3c4da2980d043c95) +46
x59d6a4fc5007b7a4.xbfac429e165a6a0d.x47f176deff0d42e2() +51
x59d6a4fc5007b7a4.x4f38812d0d5e7231.x4fb8d507f4b3c96e(Font x0078185e1040c523, String xb41faee6912a2313, x000f21cbda739311 xcb075c7088c3b520) +727
xf989f31a236ff98c.x08802e9e984cd3ee.xc325f08e9c7ac384() +47
xf989f31a236ff98c.x08802e9e984cd3ee.xd22cb714335f8d2c(String xb41faee6912a2313, Font x26094932cf7a9139) +557
x59d6a4fc5007b7a4.x4f38812d0d5e7231…ctor(String text, x1d1dd20018fcde10 font, xacc55eb1e4595209 converter) +352
x59d6a4fc5007b7a4.x42f2a218ce59dafb.xbd6559e9bd341cd6(Inline xda5bf54deb817e37, Int32 x10aaa7cdfa38f254, Int32 x961016a387451f05) +199
x59d6a4fc5007b7a4.x487cdc969fefe3d6.x4e33137198fdb9ca(Boolean xcd31b50c43a96e21) +63
x59d6a4fc5007b7a4.x487cdc969fefe3d6.x47f176deff0d42e2() +544
x59d6a4fc5007b7a4.xcde671c53995c411.xd9db07500873ae98(Document x3664041d21d73fdc, xdeb77ea37ad74c56 xdfde339da46db651) +290
Aspose.Words.Document.UpdatePageLayout() +24
xfbd1009a0cbb9842.xe70a81b27884f7a0.xb333e1e6c01c2be2() +12
xfbd1009a0cbb9842.xbf9ddf72e1283af9.x18dfca7c5fd2402f() +108
xfbd1009a0cbb9842.xfedf115fd9c03862.xdd6cf0348a23f220(xcf417e2db4fe9ed3 xe00c282e1a49fcfb) +96
xfbd1009a0cbb9842.xfedf115fd9c03862.x118a2acc122f2bb3() +29
xe86f37adaccef1c3.xc5c3f438428cb03b.xdeeb682062ef79a5() +268
xe86f37adaccef1c3.xc5c3f438428cb03b.xd5da23b762ce52a2(MailMerge x2a99de81295e7f29, IMailMergeDataSource xef1769c4fe6ae4ca, Boolean xc9c7b90943167aed) +51
Aspose.Words.Reporting.MailMerge.Execute(IMailMergeDataSource dataSource) +49
Aspose.Words.Reporting.MailMerge.Execute(String[] fieldNames, Object[] values) +44
EventM.ReportGen.Generate() +910

The /Fonts subdirectory has been confirmed to have Read Write and Execute;

Microsoft says:

To resolve the problem, replace the SystemInformation.UserName property with System.Environment.UserName.

Hi

Thank you for additional information. As I can see from stack trace this is a security problem. So you should recheck whether your application can read from the specified folder.

Also, have you tried to run the same code in a separate stand-alone application? Does it throw the same exception?

Best regards,

I confirmed that the application has Read Write and Execute in the target environment. The issue appears to be one of only having Medium Trust rights in target environment.

Hi

Thank you for more information. Aspose.Words is recommended to run with Full Trust permission set. Please see the following link for more information:
https://docs.aspose.com/words/net/aspose-export-to-pdf-for-asp-net-mvc/

Best regards,

So, after doing some additional research, I have found the following:

  • The original application using Aspose work just fine in both the local environment (development) and with our old service provider (testing) (Note: these are Aspose 8.0.0 assemblies) . It did not work in the new Network Solutions (QA & Production version 10.1.0 assemblies - necessary because of other errors and support requirements) environment.
  • The directory containing the fonts has full permissions, Read, Write & Execute
  • After significant testing, it appears that the challenge is that certain calls the Aspose assembly makes requires full trust rights.
  • Network Solutions will no longer support Full trust rights for most websites, they are supporting for certain grandfathered sites.
  • Microsoft suggest that there are other calls that can be made that utilizes only medium trust rights. They recommended that I “ask the assembly provider if they have a version of the assembly that requires only medium rights”. Does Aspose have any such assembly, or plans?

Thanks for the help and assistance.

Hi

Thank you for additional information. If you specify font’s folder in your web application, theoretically, Aspose.Words does not need full trust. I am not sure why this does not work on your side. I will test this on my side and let you know how it goes. Please expect a reply within a day.

Best regards,

Hi

Thank you for additional information. I managed to reproduce the problem with medium trust applications. We will investigate whether there is a way to use Aspose.Words in medium trust applications. Your request has been linked to the appropriate issue. We will let you know once it is resolved.

Best regards,

Is it possible to get an understanding of the functionality that is failing? The other templates are generating the proper output, but this one fails.

Hi

Thanks for your request. There is a set of restrictions when you run the application in medium trust:
https://docs.microsoft.com/en-us/previous-versions/87x8e4d1(v=vs.140)

For example Environment permissions, Aspose.Words tries to read an environment variable WINDIR. It used to build a path to fonts folder. This call might cause problems in medium trust.

Theoretically, the problem should occur with any document. On my side, I reproduced the problem with simple “Hello World” document. Maybe the templates, that work, do not have fields that require updating page layout and that is why the problem does not occur.

Best regards,

Is there any update on the status of this?

Hi

Thanks for your request. Status of the issue is “Work in Progress”. The issue is scheduled to be resolved before the next release. But I cannot 100% promise that the fix will be included into the next release. We will keep you informed and let you know once it is fixed.

Best regards,

Hello.

Unfortunately, investigation shows the current Aspose.Words code (the rendering part) cannot be executed with “Medium Trust” security settings due to some indirect .NET calls to unmanaged memory which are used in GDI+ image processing.

The issues you have found earlier (filed as WORDSNET-4912) have been fixed in this .NET update and in this Java update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.