Argument Null Exception

Hello again, I am having a lot of questions today, so here is my next issue:
the following error:
ArgumentNullException: Value cannot be null.
Parameter name: key
System.Collections.Generic.Dictionary2[TKey,TValue].FindEntry (TKey key) (at <51ee0c51c90047b488b10b1b78b365d8>:0) System.Collections.Generic.Dictionary2[TKey,TValue].ContainsKey (TKey key) (at <51ee0c51c90047b488b10b1b78b365d8>:0)
RPG.InventorySystem.InventoryItem.GetFromID (System.String itemID) (at Assets/MyAssets/Scripts/Inventories/InventoryItem.cs:54)
RPG.InventorySystem.Inventory.RPG.Saving.ISaveable.RestoreState (System.Object state) (at Assets/MyAssets/Scripts/Inventories/Inventory.cs:221)
RPG.Saving.SaveableEntity.RestoreState (System.Object state) (at Assets/MyAssets/Scripts/Saving/SaveableEntity.cs:35)
RPG.Saving.SavingSystem.RestoreState (System.Collections.Generic.Dictionary`2[TKey,TValue] state) (at Assets/MyAssets/Scripts/Saving/SavingSystem.cs:77)
RPG.Saving.SavingSystem+d__0.MoveNext () (at Assets/MyAssets/Scripts/Saving/SavingSystem.cs:24)
UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at <579f6a25593149bdb5b2b685692d23ea>:0)

and it appears to point to the following code:

        public static InventoryItem GetFromID(string itemID)
        {
            if (itemLookupCache == null)
            {
                itemLookupCache = new Dictionary<string, InventoryItem>();
                var itemList = Resources.LoadAll<InventoryItem>("");
                foreach (var item in itemList)
                {
                    if (itemLookupCache.ContainsKey(item.itemID))
                    {
                        Debug.LogError(string.Format("Looks like there's a duplicate GameDevTV.UI.InventorySystem ID for objects: {0} and {1}", itemLookupCache[item.itemID], item));
                        continue;
                    }

                    itemLookupCache[item.itemID] = item;
                }
            }

            if (itemID == null || !itemLookupCache.ContainsKey(itemID)) return null;
            return itemLookupCache[itemID];
        }
        /// <summary>

As well as:

        {
            var slotRecords = (InventorySlotRecord[])state;
            for (int i = 0; i < inventorySize; i++)
            {
                slots[i].item = InventoryItem.GetFromID(slotRecords[i].itemID);
                slots[i].number = slotRecords[i].number;
            }
            if (inventoryUpdated != null)
            {
                inventoryUpdated();
            }
        }

Thank you once again in advance!

Update: This was fixed by naming the weapons and replacing their ID’s. Why? No clue, but it worked!

1 Like

Usually this is because the ItemID hasn’t been properly initialized on the InventoryItem. So when CaptureState returns the InventoryItem’s InventoryID, it saves a null string, which… completely breaks a Dictionary that requires that any object used as an indexer into the Dictionary cannot be null.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms