Dictionary.TryGetValue throwing an error DivideByZeroException: Division by zero

What’s wrong with my Key and Value? I do not know, never seen it. Where is the cause of this error? Can someone tell me.

StatTypes is an enum, StatBase is a class.

[SerializeField] private Dictionary<StatTypes, StatBase> _statDictionary;

      public bool TryGetStat(StatTypes type, out StatBase stat)
                    lock (_statDictionary)
                        if (_statDictionary.TryGetValue(type, out stat))  //error at line 99
                            return true;
                    return false;

Update: I used ConcurrentDictionary instead. It throws a similar error at line 1570.
I can not afford to understand this script.

bucketNo = (hashcode & 0x7fffffff) % bucketCount;

DivideByZeroException: Division by zero
System.Collections.Generic.Dictionary`2[GameSystems.StatSystem.StatTypes,GameSystems.StatSystem.StatBase].TryGetValue (StatTypes key, GameSystems.StatSystem.StatBase& value) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:592)
GameSystems.StatSystem.StatDictionary.TryGetStat (StatTypes type, GameSystems.StatSystem.StatBase& stat) (at Assets/Scripts/GameSystems/StatSystems/StatDictionary/StatDictionary.cs:99)
GameSystems.StatSystem.EntityStatDictionary.get_Health () (at Assets/Scripts/GameSystems/StatSystems/StatDictionary/EntityStatDictionary.cs:40)
GameSystems.StatSystem.EntityStatDictionary.SetupLinkers () (at Assets/Scripts/GameSystems/StatSystems/StatDictionary/EntityStatDictionary.cs:313)
GameSystems.EntitySystems.EntityComponent.Load () (at Assets/Scripts/GameSystems/EntitySystems/EntityComponent.cs:172)
GameSystems.EntitySystems.EntityComponent.get_Entity () (at Assets/Scripts/GameSystems/EntitySystems/EntityComponent.cs:35)
GameSystems.EntitySystems.EntityComponent.Start () (at Assets/Scripts/GameSystems/EntitySystems/EntityComponent.cs:20)

Well, it seems the reason is that FullSerializer you’re using. The line that throws the division by zero can only throw that exception when “bucketCount” is “0”. However bucketCount comes from the internal bucket array length. When using any constructor of that ConcurrentDictionary the bucket array is initialized with the default capacity. So that internal array has never a length of 0. However that serializer framework uses reflection to save / restore even internal fields inside other classes. So it somehow manages to deserialize the Dictionary with an empty bucket array.

So I would say it’s most likely a bug in that FullSerializer that you’re using. I won’t dig any deeper into this. You may want to try to create a minimum reproduction case and contact the FullSerializer author. Don’t forget to mention the exact classes that you’re using.

How is StateTypes GetHashCode implemented? Are you maybe dividing by zero in there?

Or are you using a custom key comparer? Same potential issue in GetHashCode.

@Bunny83 Can you help me?