EvotecIT / OfficeIMO

Fast and easy to use cross-platform .NET library that creates or modifies Microsoft Word (DocX) and later also Excel (XLSX) files without installing any software. Library is based on Open XML SDK
MIT License
280 stars 50 forks source link

Adding Watermark to section that has not initialized headers will throw an error #27

Closed PrzemyslawKlys closed 1 year ago

PrzemyslawKlys commented 2 years ago

Error thrown:

image

System.NullReferenceException HResult=0x80004003 Message=Object reference not set to an instance of an object. Source=OfficeIMO.Word StackTrace: at OfficeIMO.Word.WordWatermark..ctor(WordDocument wordDocument, WordSection wordSection, WordHeader wordHeader, WordWatermarkStyle style, String text) in C:\Support\GitHub\OfficeIMO\OfficeIMO.Word\WordWatermark.cs:line 98

rstm-sf commented 1 year ago

Would you like to start supporting NRT? Update a codebase with nullable reference types to improve null diagnostic warnings

PrzemyslawKlys commented 1 year ago

I've read the document - and I have no clue what I am deciding on about 🤣🤣 and how it impacts this project or this particular issue. I am noob when it comes to programming so you need to treat me as such 😊

rstm-sf commented 1 year ago

In general, this does not solve this issue :), but directs we to take care of NullReferenceException before an error occurs at runtime. NRT only helps to write safer code around NullReferenceException

PrzemyslawKlys commented 1 year ago

I already have those warnings

image

I have both Visual Studio Enterprise and Resharper so one of those tells me to fix it anyways, but I do ignore it from time to time. I doubt in this case even NRT would help as it would have to know that _header may not exists at all if it's first not added to the document.

This particular issue will require improvement to create a header if it doesn't exist if Watermark requires, not just to fight null.

rstm-sf commented 1 year ago

No, not exactly, if wordHeader had been marked as a possible null, then the analyzers in the line would have noticed the problem even earlier -- by wordHeader. Or vice versa, mark it as not possible zero, and then, by contract, passing the parameter, the user must already ensure that wordHeader will not be null