[SOLVED] nullptr in TArray crashes ?

Hey to all.
I had asked about this in a previous thread but it has been a while so I wanted to make a new thread.
Making an Inventory system for a RPG game. Want to remove items from player’s inventory (when used or dropped) But I want to keep the indieces of course so in the UI things don’t shift when items are removed.
Someone suggested making a default nullptr value and replacing the removed item with that one so the order of array is preserved. I tried but I am getting crashes: Access violation - code c0000005 (first/second chance not available) a common nllptr crash). I get this with usually pointer errors.
Here is what I do :


      
        FInventoryItem* EmptyInventorySlot = nullptr;

        Inventory[0] = *EmptyInventorySlot;
        UpdateInventory();


This is the question and before I mentioned

https://forums.unrealengine.com/deve…changing-index

I use 4.18 and VS 2017.
I can give more details if it is required.
Thank you very much.

You’re trying to deference a nullptr which is the reason for the crash, a fundamental no-no in C++.

I’m assuming your array doesn’t store pointers, in which case the correct code is this (this also assumes you have already added elements to the array, otherwise you’ll get an out-of-bounds crash as well.



Inventory[0] = FInventoryItem();


Thanks mate you are right.
I solved it with an empty FInventoryItem and adding it to Inventory.

Solved.

https://forums.unrealengine.com/deve…changing-index