0

Hi everyone I am developing a Word add in, and everything is working except when I run the debugger it states that this line:

        return node.Attribute("ID").Value;

Is throwing a NullReferenceException, here is the code in the context of the whole program:

using System;
using System.Linq;
using System.Xml.Linq;
using OneNote = Microsoft.Office.Interop.OneNote;
using Microsoft.Office.Interop.OneNote;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Microsoft.Office.Interop.Word;

namespace WordAddIn1
{
    public partial class ThisAddIn
    {

        //creates a new OneNote Application object
        static OneNote.Application onenoteApp;
        static XNamespace ns = null;
        static string resultWordText = "";

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            onenoteApp = new OneNote.Application();
            getWordText();
            writeToOneNote();
        }

        public static void getWordText()
        {
            try
            {
                Microsoft.Office.Interop.Word.Application msWordApp = new Microsoft.Office.Interop.Word.Application();
                object nullObj = System.Reflection.Missing.Value;
                object oFalse = false;
                object oFile = "C:\\Users\\t-aaalle\\Desktop\\GreetingFile.docx";

                Microsoft.Office.Interop.Word.Document doc = msWordApp.Documents.Open(
                                                             ref oFile, ref nullObj, ref nullObj,
                                                             ref nullObj, ref nullObj, ref nullObj,
                                                             ref nullObj, ref nullObj, ref nullObj,
                                                             ref nullObj, ref nullObj, ref nullObj,
                                                             ref nullObj, ref nullObj, ref nullObj,
                                                             ref nullObj);
                resultWordText = doc.Content.Text.Trim();
                doc.Close(ref oFalse, ref nullObj, ref nullObj);
                Console.WriteLine(resultWordText);
                Console.Read();
                Process.Start("onenote.exe");

            }
            catch (Exception e)
            {
                var trace = new System.Diagnostics.StackTrace(e);
            }
        }

        public static void writeToOneNote()
        {
            GetNamespace();
            //Gets the notebook, section, and firstpage
            string notebookId = GetObjectId(null, OneNote.HierarchyScope.hsNotebooks, "Aaron @ Microsoft");
            string sectionId = GetObjectId(notebookId, OneNote.HierarchyScope.hsSections, "New Section 1");
            string firstPageId = GetObjectId(sectionId, OneNote.HierarchyScope.hsPages, "My Page");

            //Reads the contents of the first page
            GetPageContent(firstPageId);
            Console.Read();
        }


        //Gets the name of the notebook 
        static void GetNamespace()
        {
            string xml = "";

            onenoteApp.GetHierarchy(null, OneNote.HierarchyScope.hsNotebooks, out xml);
            var doc = XDocument.Parse(xml);
            ns = doc.Root.Name.Namespace;
        }


        //Gets the OneNote document
        static string GetObjectId(string parentId, HierarchyScope scope, string objectName)
        {
            string xml = "";

            onenoteApp.GetHierarchy(parentId, scope, out xml);

            var doc = XDocument.Parse(xml);
            var nodeName = "";

            switch (scope)
            {
                case (HierarchyScope.hsNotebooks): nodeName = "Notebook"; break;
                case (HierarchyScope.hsPages): nodeName = "Page"; break;
                case (HierarchyScope.hsSections): nodeName = "Section"; break;
                default:
                    return null;
            }

            var node = doc.Descendants(ns + nodeName).Where(n => n.Attribute("name").Value == objectName).FirstOrDefault();

            return node.Attribute("ID").Value; //THIS IS THE LINE
        }

        //Get's the pages content 
        static string GetPageContent(string pageId)
        {
            string xml;
            onenoteApp.GetPageContent(pageId, out xml, OneNote.PageInfo.piAll);
            var doc = XDocument.Parse(xml);
            var outLine = doc.Descendants(ns + "Outline").First();
            var content = outLine.Descendants(ns + "T").First();
            string contentVal = content.Value;
            content.Value = resultWordText;
            onenoteApp.UpdatePageContent(doc.ToString());
            return null;
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }

        #endregion
    }
}

Could someone tell me why this is happening when I run this in the debugger it says there is an ID value being passed into the Value, but then it gives me this error which doesn't make sense. Help identifying the problem would be fantastic!

  • Have you checked to ensure that `node` is not null? What about the result of `Attribute("ID")` - is that null? – BJ Myers Jul 28 '17 at 15:57
  • Either `node` is `null` or `node.Attribute("ID")` is returning `null`. – JuanR Jul 28 '17 at 15:58
  • ID does not return null it returns what it's supposed to which is strange... –  Jul 28 '17 at 16:01
  • Looking at the debugger again, it says that node is null –  Jul 28 '17 at 16:05
  • I looked at that article above and followed it's steps that's how I was able to decide where the null pointer was. –  Jul 28 '17 at 16:07

0 Answers0