Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix nullable references #168

Merged
merged 10 commits into from
Nov 6, 2023
4 changes: 2 additions & 2 deletions samples/presentation/add_comment/cs/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,14 @@ static SlidePart GetFirstSlide(PresentationDocument? presentationDocument)
string? relId = slideId?.RelationshipId;
if (relId is null)
{
throw new NullReferenceException("The first slide does not contain a relationship ID.");
throw new ArgumentNullException("The first slide does not contain a relationship ID.");
}
// Get the slide part by the relationship ID.
SlidePart? slidePart = part?.GetPartById(relId) as SlidePart;

if (slidePart is null)
{
throw new NullReferenceException("The slide part is null.");
throw new ArgumentNullException("The slide part is null.");
}

return slidePart;
Expand Down
5 changes: 3 additions & 2 deletions samples/word/accept_all_revisions/cs/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using System;
using System.Collections.Generic;
using System.Linq;

Expand All @@ -12,7 +13,7 @@ static void AcceptAllRevisions(string fileName, string authorName)
{
if (wdDoc.MainDocumentPart is null || wdDoc.MainDocumentPart.Document.Body is null)
{
throw new System.NullReferenceException("MainDocumentPart and/or Body is null.");
throw new ArgumentNullException("MainDocumentPart and/or Body is null.");
}

Body body = wdDoc.MainDocumentPart.Document.Body;
Expand Down Expand Up @@ -66,7 +67,7 @@ static void AcceptAllRevisions(string fileName, string authorName)
}
else
{
OpenXmlElement? nextSibling = insertion.NextSibling() ?? throw new System.NullReferenceException("NextSibling is null.");
OpenXmlElement? nextSibling = insertion.NextSibling() ?? throw new ArgumentNullException("NextSibling is null.");
twsouthwick marked this conversation as resolved.
Show resolved Hide resolved
nextSibling.InsertAfterSelf(new Run(run.OuterXml));
}
}
Expand Down
3 changes: 2 additions & 1 deletion samples/word/add_tables/cs/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using System;

AddTable(args[0], args[1]);

Expand All @@ -17,7 +18,7 @@ static void AddTable(string fileName, string json)
{
if (document.MainDocumentPart is null || document.MainDocumentPart.Document.Body is null)
{
throw new System.NullReferenceException("MainDocumentPart and/or Body is null.");
throw new ArgumentNullException("MainDocumentPart and/or Body is null.");
}

var doc = document.MainDocumentPart.Document;
Expand Down
3 changes: 2 additions & 1 deletion samples/word/change_text_a_table/cs/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using System;
using System.Linq;

ChangeTextInCell(args[0], args[1]);
Expand All @@ -13,7 +14,7 @@ static void ChangeTextInCell(string filePath, string txt)
{
if (doc.MainDocumentPart is null || doc.MainDocumentPart.Document.Body is null)
{
throw new System.NullReferenceException("MainDocumentPart and/or Body is null.");
throw new ArgumentNullException("MainDocumentPart and/or Body is null.");
}

// Find the first table in the document.
Expand Down
9 changes: 5 additions & 4 deletions samples/word/change_the_print_orientation/cs/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using System;
using System.Linq;

SetPrintOrientation(args[0], args[1]);
Expand All @@ -25,7 +26,7 @@ static void SetPrintOrientation(string fileName, string no)

if (document.MainDocumentPart is null)
{
throw new System.NullReferenceException("MainDocumentPart and/or Body is null.");
throw new ArgumentNullException("MainDocumentPart and/or Body is null.");
}

var docPart = document.MainDocumentPart;
Expand All @@ -37,7 +38,7 @@ static void SetPrintOrientation(string fileName, string no)
{
bool pageOrientationChanged = false;

PageSize pgSz = (sectPr.Descendants<PageSize>().FirstOrDefault()) ?? throw new System.NullReferenceException("There are no PageSize elements in the section.");
PageSize pgSz = (sectPr.Descendants<PageSize>().FirstOrDefault()) ?? throw new ArgumentNullException("There are no PageSize elements in the section.");
twsouthwick marked this conversation as resolved.
Show resolved Hide resolved
if (pgSz != null)
{
// No Orient property? Create it now. Otherwise, just
Expand Down Expand Up @@ -78,7 +79,7 @@ static void SetPrintOrientation(string fileName, string no)
pgSz.Width = height;
pgSz.Height = width;

PageMargin pgMar = (sectPr.Descendants<PageMargin>().FirstOrDefault()) ?? throw new System.NullReferenceException("There are no PageMargin elements in the section.");
PageMargin pgMar = (sectPr.Descendants<PageMargin>().FirstOrDefault()) ?? throw new ArgumentNullException("There are no PageMargin elements in the section.");

if (pgMar != null)
{
Expand All @@ -89,7 +90,7 @@ static void SetPrintOrientation(string fileName, string no)
// procedure.
if (pgMar.Top is null || pgMar.Bottom is null || pgMar.Left is null || pgMar.Right is null)
{
throw new System.NullReferenceException("One or more of the PageMargin elements is null.");
throw new ArgumentNullException("One or more of the PageMargin elements is null.");
}

var top = pgMar.Top.Value;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using System;
using System.IO;

ConvertDOCMtoDOCX(args[0]);
Expand All @@ -13,7 +14,7 @@ static void ConvertDOCMtoDOCX(string fileName)
using (WordprocessingDocument document = WordprocessingDocument.Open(fileName, true))
{
// Access the main document part.
var docPart = document.MainDocumentPart ?? throw new System.NullReferenceException("MainDocumentPart is null.");
var docPart = document.MainDocumentPart ?? throw new ArgumentNullException("MainDocumentPart is null.");

// Look for the vbaProject part. If it is there, delete it.
var vbaPart = docPart.VbaProjectPart;
Expand Down
3 changes: 2 additions & 1 deletion samples/word/create_and_add_a_character_style/cs/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using System;

CreateAndAddCharacterStyle(args[0], args[1], args[2], args[3]);

Expand Down Expand Up @@ -61,7 +62,7 @@ static StyleDefinitionsPart AddStylesPartToPackage(WordprocessingDocument? doc)

if (doc?.MainDocumentPart is null)
{
throw new System.NullReferenceException("MainDocumentPart is null.");
throw new ArgumentNullException("MainDocumentPart is null.");
}

part = doc.MainDocumentPart.AddNewPart<StyleDefinitionsPart>();
Expand Down
3 changes: 2 additions & 1 deletion samples/word/create_and_add_a_paragraph_style/cs/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using System;

CreateAndAddParagraphStyle(args[0], args[1], args[2], args[3]);

Expand Down Expand Up @@ -81,7 +82,7 @@ static StyleDefinitionsPart AddStylesPartToPackage(WordprocessingDocument? doc)

if (doc?.MainDocumentPart is null)
{
throw new System.NullReferenceException("MainDocumentPart is null.");
throw new ArgumentNullException("MainDocumentPart is null.");
}

part = doc.MainDocumentPart.AddNewPart<StyleDefinitionsPart>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ static void DeleteComments(string fileName, string author = "")

if (document.MainDocumentPart is null || document.MainDocumentPart.WordprocessingCommentsPart is null)
{
throw new System.NullReferenceException("MainDocumentPart and/or WordprocessingCommentsPart is null.");
throw new ArgumentNullException("MainDocumentPart and/or WordprocessingCommentsPart is null.");
}

// Set commentPart to the document WordprocessingCommentsPart,
Expand Down
3 changes: 2 additions & 1 deletion samples/word/extract_styles/cs/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DocumentFormat.OpenXml.Packaging;
using System;
using System.IO;
using System.Xml;
using System.Xml.Linq;
Expand All @@ -24,7 +25,7 @@ static XDocument ExtractStylesPart(string fileName, string getStylesWithEffectsP
{
if (document.MainDocumentPart is null || document.MainDocumentPart.StyleDefinitionsPart is null || document.MainDocumentPart.StylesWithEffectsPart is null)
{
throw new System.NullReferenceException("MainDocumentPart and/or one or both of the Styles parts is null.");
throw new ArgumentNullException("MainDocumentPart and/or one or both of the Styles parts is null.");
}

// Get a reference to the main document part.
Expand Down
4 changes: 2 additions & 2 deletions samples/word/insert_a_comment/cs/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ static void AddCommentOnFirstParagraph(string fileName, string author, string in
{
if (document.MainDocumentPart is null/* || document.MainDocumentPart.WordprocessingCommentsPart is null*/)
{
throw new System.NullReferenceException("MainDocumentPart and/or Body is null.");
throw new ArgumentNullException("MainDocumentPart and/or Body is null.");
}

WordprocessingCommentsPart wordprocessingCommentsPart = document.MainDocumentPart.WordprocessingCommentsPart ?? document.MainDocumentPart.AddNewPart<WordprocessingCommentsPart>();
Expand All @@ -39,7 +39,7 @@ static void AddCommentOnFirstParagraph(string fileName, string author, string in
}
else
{
throw new System.NullReferenceException("Comment id and/or value are null.");
throw new ArgumentNullException("Comment id and/or value are null.");
}
})
.Max() + 1).ToString();
Expand Down
5 changes: 3 additions & 2 deletions samples/word/insert_a_picture/cs/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using System;
using System.IO;
using A = DocumentFormat.OpenXml.Drawing;
using DW = DocumentFormat.OpenXml.Drawing.Wordprocessing;
Expand All @@ -14,7 +15,7 @@ static void InsertAPicture(string document, string fileName)
{
if (wordprocessingDocument.MainDocumentPart is null)
{
throw new System.NullReferenceException("MainDocumentPart is null.");
throw new ArgumentNullException("MainDocumentPart is null.");
}

MainDocumentPart mainPart = wordprocessingDocument.MainDocumentPart;
Expand Down Expand Up @@ -98,7 +99,7 @@ static void AddImageToBody(WordprocessingDocument wordDoc, string relationshipId

if (wordDoc.MainDocumentPart is null || wordDoc.MainDocumentPart.Document.Body is null)
{
throw new System.NullReferenceException("MainDocumentPart and/or Body is null.");
throw new ArgumentNullException("MainDocumentPart and/or Body is null.");
}

// Append the reference to body, the element should be in a Run.
Expand Down
3 changes: 2 additions & 1 deletion samples/word/insert_a_table/cs/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using System;

CreateTable(args[0]);

Expand Down Expand Up @@ -88,7 +89,7 @@ static void CreateTable(string fileName)

if (doc.MainDocumentPart is null || doc.MainDocumentPart.Document.Body is null)
{
throw new System.NullReferenceException("MainDocumentPart and/or Body is null.");
throw new ArgumentNullException("MainDocumentPart and/or Body is null.");
}

// Append the table to the document.
Expand Down
3 changes: 2 additions & 1 deletion samples/word/remove_hidden_text/cs/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DocumentFormat.OpenXml.Packaging;
using System;
using System.IO;
using System.Xml;

Expand All @@ -18,7 +19,7 @@ static void WDDeleteHiddenText(string docName)

if (wdDoc.MainDocumentPart is null || wdDoc.MainDocumentPart.Document.Body is null)
{
throw new System.NullReferenceException("MainDocumentPart and/or Body is null.");
throw new ArgumentNullException("MainDocumentPart and/or Body is null.");
}

// Get the document part from the package.
Expand Down
3 changes: 2 additions & 1 deletion samples/word/remove_the_headers_and_footers/cs/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using System;
using System.Linq;

RemoveHeadersAndFooters(args[0]);
Expand All @@ -13,7 +14,7 @@ static void RemoveHeadersAndFooters(string filename)
{
if (doc.MainDocumentPart is null)
{
throw new System.NullReferenceException("MainDocumentPart is null.");
throw new ArgumentNullException("MainDocumentPart is null.");
}

// Get a reference to the main document part.
Expand Down
7 changes: 4 additions & 3 deletions samples/word/replace_the_header/cs/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using System;
using System.Collections.Generic;
using System.Linq;

Expand All @@ -13,12 +14,12 @@ static void AddHeaderFromTo(string filepathFrom, string filepathTo)
{
if (wdDocSource.MainDocumentPart is null || wdDocSource.MainDocumentPart.HeaderParts is null)
{
throw new System.NullReferenceException("MainDocumentPart and/or HeaderParts is null.");
throw new ArgumentNullException("MainDocumentPart and/or HeaderParts is null.");
}

if (wdDoc.MainDocumentPart is null)
{
throw new System.NullReferenceException("MainDocumentPart is null.");
throw new ArgumentNullException("MainDocumentPart is null.");
}

MainDocumentPart mainPart = wdDoc.MainDocumentPart;
Expand All @@ -45,7 +46,7 @@ static void AddHeaderFromTo(string filepathFrom, string filepathTo)

if (mainPart.Document.Body is null)
{
throw new System.NullReferenceException("Body is null.");
throw new ArgumentNullException("Body is null.");
}

// Get SectionProperties and Replace HeaderReference with new Id.
Expand Down
5 changes: 3 additions & 2 deletions samples/word/replace_the_styles_parts/cs/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DocumentFormat.OpenXml.Packaging;
using System;
using System.IO;
using System.Xml;
using System.Xml.Linq;
Expand Down Expand Up @@ -33,7 +34,7 @@ static void ReplaceStylesPart(string fileName, XDocument newStyles, bool setStyl
{
if (document.MainDocumentPart is null || document.MainDocumentPart.StyleDefinitionsPart is null || document.MainDocumentPart.StylesWithEffectsPart is null)
{
throw new System.NullReferenceException("MainDocumentPart and/or one or both of the Styles parts is null.");
throw new ArgumentNullException("MainDocumentPart and/or one or both of the Styles parts is null.");
}

// Get a reference to the main document part.
Expand Down Expand Up @@ -71,7 +72,7 @@ static XDocument ExtractStylesPart(string fileName, bool getStylesWithEffectsPar

if (docPart is null || docPart.StyleDefinitionsPart is null || docPart.StylesWithEffectsPart is null)
{
throw new System.NullReferenceException("MainDocumentPart and/or one or both of the Styles parts is null.");
throw new ArgumentNullException("MainDocumentPart and/or one or both of the Styles parts is null.");
}

// Assign a reference to the appropriate part to the
Expand Down
3 changes: 2 additions & 1 deletion samples/word/replace_the_theme_part/cs/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DocumentFormat.OpenXml.Packaging;
using System;
using System.IO;

ReplaceTheme(args[0], args[1]);
Expand All @@ -10,7 +11,7 @@ static void ReplaceTheme(string document, string themeFile)
{
if (wordDoc.MainDocumentPart is null || wordDoc.MainDocumentPart.Document.Body is null || wordDoc.MainDocumentPart.ThemePart is null)
{
throw new System.NullReferenceException("MainDocumentPart and/or Body and/or ThemePart is null.");
throw new ArgumentNullException("MainDocumentPart and/or Body and/or ThemePart is null.");
}

MainDocumentPart mainPart = wordDoc.MainDocumentPart;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ static void GetApplicationProperty(string fileName)
{
if (document.ExtendedFilePropertiesPart is null)
{
throw new System.NullReferenceException("ExtendedFilePropertiesPart is null.");
throw new ArgumentNullException("ExtendedFilePropertiesPart is null.");
}

var props = document.ExtendedFilePropertiesPart.Properties;
Expand Down
2 changes: 1 addition & 1 deletion samples/word/retrieve_comments/cs/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ static void GetCommentsFromDocument(string fileName)
{
if (wordDoc.MainDocumentPart is null || wordDoc.MainDocumentPart.WordprocessingCommentsPart is null)
{
throw new System.InvalidOperationException("MainDocumentPart and/or WordprocessingCommentsPart is null.");
throw new System.ArgumentNullException("MainDocumentPart and/or WordprocessingCommentsPart is null.");
}

WordprocessingCommentsPart commentsPart = wordDoc.MainDocumentPart.WordprocessingCommentsPart;
Expand Down
3 changes: 2 additions & 1 deletion samples/word/search_and_replace_text_a_part/cs/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DocumentFormat.OpenXml.Packaging;
using System;
using System.IO;
using System.Text.RegularExpressions;

Expand All @@ -14,7 +15,7 @@ static void SearchAndReplace(string document)

if (wordDoc.MainDocumentPart is null)
{
throw new System.NullReferenceException("MainDocumentPart and/or Body is null.");
throw new ArgumentNullException("MainDocumentPart and/or Body is null.");
}

using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream()))
Expand Down
Loading