Weird bug with RemoveItem

Hi there! I've run into a weird bug I haven't been able to narrow down – I'm entirely sure I did something to introduce this issue, but it seems to be occurring throughout the Inventory system and not just in one place. Anytime I call RemoveItem, I'm getting a null reference exception on the following line of code:

C#:
            if (m_Inventory != null) {
                EventHandler.ExecuteEvent<ItemInfo>(m_Inventory, EventNames.c_Inventory_OnRemove_ItemInfo,
                    (itemInfo.Item, itemInfo.Amount, this, itemStackToRemove));
            }

I did a debug on each item in that line, and they are all coming back with results...

Here is the console log:
Code:
NullReferenceException: Object reference not set to an instance of an object
Opsive.Shared.Events.EventHandler.ExecuteEvent[T1] (System.Object obj, System.String eventName, T1 arg1) (at <27da9e1afec54f2fb2a11d46a234f9df>:0)
Opsive.UltimateInventorySystem.Core.InventoryCollections.ItemCollection.RemoveInternal (Opsive.UltimateInventorySystem.Core.DataStructures.ItemInfo itemInfo) (at Assets/Opsive/UltimateInventorySystem/Scripts/Core/InventoryCollections/ItemCollection.cs:598)
Opsive.UltimateInventorySystem.Core.InventoryCollections.ItemCollection.RemoveItem (Opsive.UltimateInventorySystem.Core.DataStructures.ItemInfo itemInfo) (at Assets/Opsive/UltimateInventorySystem/Scripts/Core/InventoryCollections/ItemCollection.cs:678)
Opsive.UltimateInventorySystem.Core.InventoryCollections.ItemCollection.RemoveItem (Opsive.UltimateInventorySystem.Core.Item item, System.Int32 amount) (at Assets/Opsive/UltimateInventorySystem/Scripts/Core/InventoryCollections/ItemCollection.cs:689)
Opsive.UltimateInventorySystem.Crafting.Processors.SimpleCraftingProcessor.RemoveIngredients (Opsive.UltimateInventorySystem.Core.InventoryCollections.IInventory inventory, Opsive.Shared.Utility.ListSlice`1[T] selectedIngredients) (at Assets/Opsive/UltimateInventorySystem/Scripts/Crafting/Processors/SimpleCraftingProcessor.cs:475)
Opsive.UltimateInventorySystem.Crafting.Processors.SimpleCraftingProcessorWithTime.CraftInternal (Opsive.UltimateInventorySystem.Crafting.CraftingRecipe recipe, Opsive.UltimateInventorySystem.Core.InventoryCollections.IInventory inventory, Opsive.Shared.Utility.ListSlice`1[T] selectedIngredients, System.Int32 quantity) (at Assets/Scripts/Crafting/SimpleCraftingProcessorWithTime.cs:112)
Opsive.UltimateInventorySystem.Crafting.CraftingProcessor.Craft (Opsive.UltimateInventorySystem.Crafting.CraftingRecipe recipe, Opsive.UltimateInventorySystem.Core.InventoryCollections.IInventory inventory, Opsive.Shared.Utility.ListSlice`1[T] selectedIngredients, System.Int32 quantity) (at Assets/Opsive/UltimateInventorySystem/Scripts/Crafting/CraftingProcessor.cs:139)
Opsive.UltimateInventorySystem.Crafting.CraftingProcessor.Craft (Opsive.UltimateInventorySystem.Crafting.CraftingRecipe recipe, Opsive.UltimateInventorySystem.Core.InventoryCollections.IInventory inventory, System.Int32 quantity) (at Assets/Opsive/UltimateInventorySystem/Scripts/Crafting/CraftingProcessor.cs:122)
Opsive.UltimateInventorySystem.UI.Menus.Crafting.CraftingMenu.CraftSelectedQuantity () (at Assets/Opsive/UltimateInventorySystem/Scripts/UI/Menus/Crafting/CraftingMenu.cs:245)
Opsive.UltimateInventorySystem.UI.Panels.ConfirmCancelPanel.ClickedConfirm () (at Assets/Opsive/UltimateInventorySystem/Scripts/UI/Panels/ConfirmCancelPanel.cs:48)
UnityEngine.Events.InvokableCall.Invoke () (at /Users/bokken/buildslave/unity/build/Runtime/Export/UnityEvent/UnityEvent.cs:180)
UnityEngine.Events.UnityEvent.Invoke () (at /Users/bokken/buildslave/unity/build/Runtime/Export/UnityEvent/UnityEvent/UnityEvent_0.cs:58)
LongClickButton.Update () (at Assets/Scripts/UI/Crafting/LongClickButton.cs:94)

I'm 99.99% sure this error has nothing to do with my custom classes listed in this console log, because it's happening when I run a few very simple lines of code in other classes to remove an item from the inventory.

Any thoughts on where I should look for the issue?

Thanks so much!
 
What version of UIS are you using?
If I remember correctly a version or two ago there was a bug with the event system that could cause a null reference exception. Perhaps that's what's going on and it has nothing to do with your custom code.
If you can, try updating to the latest version
 
I'm a version or two back. Upgrading is such a grueling process given how much custom code I have (even though I tried my best to create custom classes and keep things separate, last time I updated it completely destroyed my inventory database). Mind recommending how I might fix the bug in the eventsystem in the interim before I upgrade down the road?
 
Unfortunatly the Event system is part of a dll. I'm not convinced you can fix it without updating the whole package.
You could try replacing just the dll with the Event system, but there is no garantee it will be compatible with the rest of the dlls or the code base....
sorry

Always make a backup before updating the package.
 
Any ideas why this bug would have started to happen when it was working fine before? Which dll has the eventsystem? I have plenty of backups that I could pull the file from :)

Thanks for the help.
 
I actually found the issue. In one of my classes where I'm listening for removed items:

C#:
Opsive.Shared.Events.EventHandler.RegisterEvent<ItemInfo, ItemStack>(m_MonitoredInventory,
            EventNames.c_Inventory_OnRemove_ItemInfo, OnItemAmountRemoved);

And in another class, when I remove an item like this:
C#:
Statics.player.GetComponent<Inventory>().MainItemCollection.RemoveItem(myItemInfo.Item, 1);

It causes the above error. For the moment, I commented out everything in the OnItemAmountRemoved function and I still get the error. What am I doing incorrectly her?
 
Unfortunatly if it is indeed a bug in the event system, there is nothing you can do outside that would help avoid it. You'll run into it eventually.
You can click on a dll to see the classes inside. It will be in the shared folder
 
Hi Sangemdoko,

I'm following up on this old thread, as I've now updated to the most current version of UIS, and I'm still getting the initial error at the top of the post. I subscribed to the event:
C#:
Opsive.Shared.Events.EventHandler.RegisterEvent<ItemInfo, ItemStack>(m_MonitoredInventory,
            EventNames.c_Inventory_OnRemove_ItemInfo, OnItemAmountRemoved);

And when any item is removed from the inventory, I get the console error at the top of this thread.

Any other ideas?
 
This should have been fixed in the last version.

Can you confirm that you imported the UIS package correctly by checking the version 1.2.10 at the bottom of the Ultimate Inventory System Editor window (not the package manager window)

Did you remove the Opsive folder before importing the update? If not try that.
 
Top