NullReferenceException when saving a file


#1

We were just about to begin User Acceptance Testing when our preliminary tests failed on a certain document. For some reason, this particular document (attached) causes an Object Reference Not Found Exception (full details pasted below). The functionality behind this is as follows:

a) A user uploads a file and this is stored away
b) Some time later, this document is retrieved, and all the sections are copied into another document.



The purpose of our app is to process word docs uploaded by anyone, so we need to process all cases (as far as that is possible). I know there must be something peculiar about this particular document as every other doc I try works perfectly; however, if I try to merge in the attached doc, it fails. If you could tell us what it is about this doc which causes the probs.


#2

Hi Jat,

This was caused by the fact that you had some styles in the source document, such as Heading 4 and did not have them in the destination document.

This is a bit of a problem when trying to move sections between documents as styles are not copied and we don’t have a good solution yet for situations where some styles are missing or different between the documents.

I’ve made a fix in Aspose.Word 1.4.3 so the styles are at least reset to Normal or NormalChar when a suitable style cannot be found in the destination document.

You can download the hotfix now. Please note that some formatting from the original document could be lost. In your case it loses list numbering, I think it is related to the style.

As far as loading any document without problems - we will get there, but only over some period of time. There is a number of poorly documented areas in Word file format and sometimes we manage to find badly formed documents - this all ensures that Aspose.Word will sometimes have problems with a document. It will throw an exception. We try to detect possible problems and at least throw useful messages, but sometimes you will just get null reference exception. We can only react and release a hotfix quickly.


#3

Thanks again for your prompt response…

I will test it out shortly.

Regarding styles, is it possible to ‘merge styles’ into the new document, with functionality as follows:

Where a style from the source document does not exist or differs in the destination doc(merge doc)
-> give the option to
a) copy the style from the source doc from to the merge doc (creating it if it doesn’t exist, or overwriting it in the merged doc if it does exist); OR
b) copy the ‘content’ from the source and apply the default style from the merged doc (i.e. don’t copy/apply any styles, just ‘content’)

So it could be something like:
mergedoc.Sections(x).appendContent(copysection, STYLES_COPY)
mergedoc.Sections(x).appendContent(copysection, STYLES_IGNORE)


What do you think?


#4

Thanks, I was thinking along similar lines. We will support this, but later. Is it critical for you not to loose style formatting? I don’t think it happens in many documents because not many people are actually changing Word styles - they just format adhoc.

To avoid having built-in styles such as Heading 4 in your example being reset to Normal, you can create a destination document and make sure all or at least most of the built in styles are used at least once. I actually think you can delete all of the text from it after you’ve used the styles and leave the document blank.


#5

Thanks Roman,

As you know, we are writing court applications, and thus many of the people who will be uploading documents are from law firms. As such, they often have their own ‘special’ templates for their firm (usually with letterheads, specific styles, etc). Further, because they are legal documents (in fact, summonses), it is important that we preserve as much formatting is possible.

I’m curious - if I create a number of empty regions and assign various styles, how will this help? Will it preserve the actual text (and just lose font characteristics such as bold/italics), or will it have the same effect as just being reset to the normal style.

Cheers,

Jat


#6

Actually, it should not lose any formatting if the style of the paragraph had to be reset to Normal because in the document the paragraph properties are “fully expanded” and it just references a style.

I will investigate why list numering was lost after moving a section to a different document.

I think we will make it working without losing any formatting. The style of the paragraph will get reset, but the formatting will stay.


#7

Sounds great, we have no need to copy the styles so long as formatting is completely preserved

Thanks Roman.


#8

List formatting is no longer list when copying between documents in Aspose.Word 2.0.4