locked
Accessing Document.AttachmentLink throws NullReferenceException RRS feed

  • Question

  • I have a class which inherits from Document.  When I create a new instance of this class in C#, any reference to the inherited AttachmentLink property results in a NullReferenceException being thrown.  Document exposes only a parameterless constructor, and the AttachmentLink property appears to only have a getter defined.  When working with a Document-derived object that I've created in code, is there anything I can do to stop it from blowing up if I try to check the value of AttachmentLink?
    Friday, October 24, 2014 8:37 PM

Answers

  • Thanks for the explanation - we will update the Document class to return nulls for AttachmentLink when not available. Meanwhile, please use the workaround to extend from Resource.
    Tuesday, November 4, 2014 3:02 AM

All replies

  • Can you please explain what your use case and how your code uses AttachmentLink? This is set when the Document is read/stored in DocumentDB. If you don't need to track attachments, you can alternatively derive from Resource instead of Document. This will help you avoid the exceptions.

    Tuesday, October 28, 2014 9:43 PM
  • I have a class (call it MyClass) that encapsulates some of the data I'm putting in DocumentDB, and I made that class inherit from Document.  To insert new data into DocumentDB, my program creates a large number of MyClass objects, populates them, and inserts them into DocumentDB using the bulk-insert stored procedure from the DocumentDB Samples C# solution released by Microsoft.

    As in the DocumentDB Sample code, I serialize all the MyClass objects into a JSON array, then deserialize that back into a dynamic object which is passed to the bulk-insert stored procedure.  To compare performance, I wrote versions of this code using the Newtonsoft and Jil JSON libraries.  The Newtonsoft version worked, but the Jil version did not.  I altered the Jil code, providing an option to tell it to serialize inherited properties (which it doesn't do by default), and then it started throwing NullReferenceExceptions.  Then I started checking object properties one by one, and found that the problem is with AttachmentLink.

    Reproducing it is pretty simple -- MyClass can be any class inheriting from Document:

    MyClass foo = new MyClass();
    Trace.WriteLine("AttachmentLink is: " + foo.AttachmentLink ?? "null");

    We don't actually need attachments for anything (yet), so I'll give it a try using Resource instead; thanks for the suggestion.

    Wednesday, October 29, 2014 5:37 PM
  • Thanks for the explanation - we will update the Document class to return nulls for AttachmentLink when not available. Meanwhile, please use the workaround to extend from Resource.
    Tuesday, November 4, 2014 3:02 AM