SharpSerializer
SharpSerializer copied to clipboard
NullReferenceException when serializing (using Version 3.0.1)
I get the following NullReferenceException when trying to serialize to binary (Burst mode):
bei Polenter.Serialization.Advanced.BinaryPropertySerializer.writeProperties(PropertyCollection properties, Type ownerType) bei Polenter.Serialization.Advanced.BinaryPropertySerializer.SerializeComplexProperty(PropertyTypeInfo
1 property) bei Polenter.Serialization.Advanced.Serializing.PropertySerializer.serializeReferenceTarget(PropertyTypeInfo
1 property) bei Polenter.Serialization.Advanced.Serializing.PropertySerializer.SerializeCore(PropertyTypeInfo1 property) bei Polenter.Serialization.Advanced.BinaryPropertySerializer.writeProperties(PropertyCollection properties, Type ownerType) bei Polenter.Serialization.Advanced.BinaryPropertySerializer.SerializeComplexProperty(PropertyTypeInfo
1 property) bei Polenter.Serialization.Advanced.Serializing.PropertySerializer.serializeReferenceTarget(PropertyTypeInfo1 property) bei Polenter.Serialization.Advanced.Serializing.PropertySerializer.SerializeCore(PropertyTypeInfo
1 property) bei Polenter.Serialization.Advanced.BinaryPropertySerializer.writeItems(ICollection1 items, Type defaultItemType) bei Polenter.Serialization.Advanced.BinaryPropertySerializer.SerializeCollectionProperty(PropertyTypeInfo
1 property) bei Polenter.Serialization.Advanced.Serializing.PropertySerializer.serializeReferenceTarget(PropertyTypeInfo1 property) bei Polenter.Serialization.Advanced.Serializing.PropertySerializer.SerializeCore(PropertyTypeInfo
1 property) bei Polenter.Serialization.Advanced.BinaryPropertySerializer.writeProperties(PropertyCollection properties, Type ownerType) bei Polenter.Serialization.Advanced.BinaryPropertySerializer.SerializeComplexProperty(PropertyTypeInfo1 property) bei Polenter.Serialization.Advanced.Serializing.PropertySerializer.serializeReferenceTarget(PropertyTypeInfo
1 property) bei Polenter.Serialization.Advanced.Serializing.PropertySerializer.SerializeCore(PropertyTypeInfo`1 property) bei Polenter.Serialization.Advanced.Serializing.PropertySerializer.Serialize(Property property) bei Polenter.Serialization.SharpSerializer.Serialize(Object data, Stream stream)
I get the same error. Using version 2.20.0 works fine.
I'm getting the same error, no clue about whats is causing it. But I can't use 2.20 since I also need .NET 4.7 SharpSerializer really would save me :(
In our case the problem was due to the fact that we had private getters and setters (probably bad copy-paste from someone :D).
This caused such properties to be included in the PropertyCollection:
private void writeProperties(PropertyCollection properties, Type ownerType)
{
// How many
_writer.WriteNumber(Convert.ToInt16(properties.Count));
// Serialize all of them
foreach (Property property in properties)
{
PropertyInfo propertyInfo = ownerType.GetProperty(property.Name);
SerializeCore(new PropertyTypeInfo<Property>(property, propertyInfo.PropertyType));
}
}
Then in the part ownerType.GetProperty(property.Name)
you will just get a null because GetProperty() can't find it.
The null pointer exception comes from propertyInfo.PropertyType
.
Removing the private getters and setters solved our problem (but still it would be nice to improve the code of SharpSerializer).
Actually, the exception might be caused by a bug that seems to be in SharpSerializer since a long time. It will fail if you try to serialize an array of simple types. For instance, try to serialize this one and you will get the bug:
public class Test
{
public int[] f { get; set; }
public Test()
{
f = new int[] { 1, 2, 3, 4, 5, 6, 7 };
}
}