SharpSerializer icon indicating copy to clipboard operation
SharpSerializer copied to clipboard

NullReferenceException when serializing (using Version 3.0.1)

Open tobiasschittkowski opened this issue 6 years ago • 4 comments

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(PropertyTypeInfo1 property) bei Polenter.Serialization.Advanced.Serializing.PropertySerializer.serializeReferenceTarget(PropertyTypeInfo1 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(PropertyTypeInfo1 property) bei Polenter.Serialization.Advanced.Serializing.PropertySerializer.serializeReferenceTarget(PropertyTypeInfo1 property) bei Polenter.Serialization.Advanced.Serializing.PropertySerializer.SerializeCore(PropertyTypeInfo1 property) bei Polenter.Serialization.Advanced.BinaryPropertySerializer.writeItems(ICollection1 items, Type defaultItemType) bei Polenter.Serialization.Advanced.BinaryPropertySerializer.SerializeCollectionProperty(PropertyTypeInfo1 property) bei Polenter.Serialization.Advanced.Serializing.PropertySerializer.serializeReferenceTarget(PropertyTypeInfo1 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(PropertyTypeInfo1 property) bei Polenter.Serialization.Advanced.Serializing.PropertySerializer.serializeReferenceTarget(PropertyTypeInfo1 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)

tobiasschittkowski avatar Jun 08 '18 11:06 tobiasschittkowski

I get the same error. Using version 2.20.0 works fine.

gave92 avatar Aug 11 '18 07:08 gave92

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 :(

caioaletroca avatar Dec 01 '18 16:12 caioaletroca

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).

rjpramos avatar Jan 16 '20 12:01 rjpramos

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 };
        }
    }

Steph55 avatar Jun 24 '20 19:06 Steph55