DataContractSerializer threw System.NullReferenceException: Object reference not set to an instance of an object..

I got the following exception during serialization of an object:
 System.NullReferenceException: Object reference not set to an instance of an object..

System.Runtime.Serialization.XmlObjectSerializerWriteContext.OnHandleIsReference(XmlWriterDelegator xmlWriter, DataContract contract, Object obj)
System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithXsiType(XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle objectTypeHandle, Type objectType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle, Type declaredType)
System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle)
System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerializeReference(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle)
WriteEventEditionToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , ClassDataContract )
System.Runtime.Serialization.ClassDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context)
System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
System.Runtime.Serialization.DataContractSerializer.InternalWriteObjectContent(XmlWriterDelegator writer, Object graph)
System.Runtime.Serialization.DataContractSerializer.InternalWriteObject(XmlWriterDelegator writer, Object graph)
System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions(XmlWriterDelegator writer, Object graph)
System.Runtime.Serialization.XmlObjectSerializer.WriteObject(XmlDictionaryWriter writer, Object graph)
System.Runtime.Serialization.XmlObjectSerializer.WriteObject(Stream stream, Object graph)
ReedExpo.Nova.UnitTesting.Events.EventHandlerTests.Test_TestSerializeEventEdition() in D:ProjectsNOVA PlatformWorkingReedExpo.NovaReedExpo.Nova.UnitTestingEventsEventHandlerTests.cs: line 85 

Turns out there were two reasons I encountered this:

  1. I hadn’t marked the type as a [DataContract] which meant public properties not marked as [DataMember] were still being serialized (I believe this happens post 3.5 SP1)
  2. More significantly, the return type of the property was a generic IList<MyType> (if I changed this to be a concrete class then the error goes away).
Advertisements

2 thoughts on “DataContractSerializer threw System.NullReferenceException: Object reference not set to an instance of an object..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s