Unity-SerializableDictionary icon indicating copy to clipboard operation
Unity-SerializableDictionary copied to clipboard

NullReferenceException in OnAfterDeserialize

Open Haaxor1689 opened this issue 5 years ago • 3 comments

I'm getting a NullReferenceException from OnAfterDeserialize method. Here is the full stack trace:

NullReferenceException: Object reference not set to an instance of an object
System.Collections.Generic.Dictionary`2[TKey,TValue].TryInsert (TKey key, TValue value, System.Collections.Generic.InsertionBehavior behavior) (at <ac823e2bb42b41bda67924a45a0173c3>:0)
System.Collections.Generic.Dictionary`2[TKey,TValue].Add (TKey key, TValue value) (at <ac823e2bb42b41bda67924a45a0173c3>:0)
SerializableDictionaryBase`3[TKey,TValue,TValueStorage].OnAfterDeserialize () (at Assets/SerializableDictionary/SerializableDictionary.cs:30)

When debugging the code it seems that both m_keys and m_values contain correct serialized values. From what I was able to find online, this could be somehow connected to the dictionary being accessed from multiple threads, which I'm not sure if it's the case with Unity's serialization system.

Here is a link to the source code and a dictionary that is giving me the exception: Dictionary Place where it is serialized

Haaxor1689 avatar May 03 '19 19:05 Haaxor1689

Hi! I could not find where you use a SerializableDictionnary in the links and the first one is not working. Can you reproduce the error on a more simple example ?

azixMcAze avatar May 15 '19 15:05 azixMcAze

Oh those links point to newest commits and I have changed the implementation since then. Here you can see the new implementation. I've implemented the ISerializationCallbackReceiver myself and different to your implementation inside OnAfterDeserialize I'm creating new instance of dictionary which doesn't throw any exceptions when adding elements to it.

Haaxor1689 avatar May 15 '19 21:05 Haaxor1689

I got this error in the case below: The dictionary's key is an object that can be null (e.g. Transform) In a script attached to a gameobject, when dictionary's key is null, it gives this exception while loading the gameobject

ilterbilguven avatar Mar 12 '20 10:03 ilterbilguven