This object is in archive! 

IMyInventory.GetAcceptedItems() throws a NullReferenceException for unconstrained inventories

TheVindicar shared this bug 5 years ago
Solved

If an inventory doesn't have a constraint set (i.e. accepts any items, like cargo container), then using GetAcceptedItems() on this inventory will throw a NullReferenceException.

Example Code:

public void Main(string argument, UpdateType updateSource)
{
    IMyTerminalBlock g = GridTerminalSystem.GetBlockWithName("Small Cargo Container");
    if (g == null)
    {
        Echo("No such block");
        return;
    }
    Echo(g.CustomName);
    List<MyItemType> items = new List<MyItemType>();
    for (int i = 0; i < g.InventoryCount; i++)
    {
        IMyInventory inv = g.GetInventory(i);
        Echo(inv.ToString());
        items.Clear();
        inv.GetAcceptedItems(items); //BOOM!
        Echo($"Inventory accepts {items.Count} types");
    }
}

Replies (3)

photo
1

Hello, Engineer!


Thank you for your feedback! Your topic has been added between considered issues.

Please keep voting for the issue as it will help us to identify the most serious bugs.


We really appreciate your patience.


Kind Regards

Keen Software House: QA Department

photo
2

And for whoever fixes this, please take a look at what GetAcceptedItems() iterates:

using (List<MyPhysicalItemDefinition>.Enumerator enumerator = MyDefinitionManager.Static.GetWeaponDefinitions().GetEnumerator())

I've tested that exact code with a mod and it only gives weapons, therefore even if this NRE is fixed the method is still useless.

photo
1

Fixed in hotfix 191.106. Release: 01.07.2019

Replies have been locked on this page!