Why are objects just set to null?

Hi,
I’m currently writing an inventory system for my game in unity and i have a little problem concerning the array in which i save my items.
To store my items in the inventory i unite items of the same type into an item stack and then i store the item stack into an one dimensional array.
The ItemStack class is a standard class which derives from nothing and my Inventory is a MonoBehaviour class and contains the array.
Here is the simplified code:
The ItemStack class basically contains the item which is stored in the stack and the amount.

public class ItemStack
{
    private Item stackItem;
    private int stackSize;

    public ItemStack(Item item, int amount)
    {
        stackItem = item;
        stackSize = amount;
    }

    public ItemStack()
    {
        stackItem = null;
        stackSize = -1;
    }
}

The Inventory class holds the array:

public class Inventory : MonoBehaviour
{
    public ItemStack[] inventory = new ItemStack[20];
}

To add a stack to the inventory i do following:

 public bool StoreInFirstFreeSlot(Item item)
    {
        for (int i = 0; i < inventorySize; i++)
        {
            if (inventory *== null)*

{
ItemStack stack = new ItemStack(item, 1);
inventory = stack;
return true;
}
}
return false;
}
Until here everything is fine. I initialize a new ItemStack and give it the item and the amount. Right after that line the item in the item stack is not null. But when i want to access the Stack later on it says that the item in the stack is null. But i can still access the methods and values of the item.
Why is the item null when i want to access it later ? I know that an object can be fake null but why is the variable stackItem even deleted or set to null in the itemStack class ?

public class Inventory : MonoBehaviour
{
public static Inventory instance;

    public int inventorySize = 20;

    public ItemStack[] inventory = new ItemStack[20];

    public delegate void OnItemChanged();
    public OnItemChanged onItemChangedCallback;

    void Awake()
	{
		if (instance != null) {
			Debug.Log ("More than one inventory found");
			return;
		}
		instance = this;
	}

    void Start()
    {
        //inventory = new ItemStack[inventorySize];
    }

    public bool Add (Item item)
	{
        List<ItemStack> itemStacks = FindStacksWith(item);

        for(int i = 0; i < itemStacks.Count; i++)
        {
            if(itemStacks*.HasFreeSpace())*

{
itemStacks*.Push(item);*
return true;
}
}
bool returnBool = StoreInFirstFreeSlot(item);
InventoryChanged();
return returnBool;
* }*

public bool Add(ItemStack stack, int i)
{
if (inventory != null) return false;
inventory = stack;
InventoryChanged();
return true;
}

public void InventoryChanged()
{
if (onItemChangedCallback != null)
{
onItemChangedCallback.Invoke();
}
}

public ItemStack FindStackWith(Item item)
{
foreach (ItemStack stack in inventory)
{
if(stack != null && stack.Peek() == item)
{
return stack;
}
}
return null;
}

public List FindStacksWith(Item item)
{
List itemStacks = new List();

for(int i = 0; i < inventory.Length; i++)
{
if (inventory != null && inventory*.Peek().itemID == item.itemID)*
{
itemStacks.Add(inventory*);*
}
}
return itemStacks;
}

public bool StoreInFirstFreeSlot(Item item)
{
for (int i = 0; i < inventorySize; i++)
{
if (inventory == null)
{
ItemStack stack = new ItemStack(item, 1);
Add(stack, i);
Debug.Log(“S.I.F.F.S” + item);
return true;
}
}
return false;
}

}
That’s the Inventory class
public class ItemStack
{
private Item stackItem;
private int stackSize;

public ItemStack(Item item, int amount)
{
stackItem = item;
stackSize = amount;
}

public ItemStack()
{
stackItem = null;
stackSize = -1;
}

public Item Peek()
{
return stackItem;
}

public bool Push(Item item)
{
Debug.Log(“” + item.itemID);
if(stackItem == null)
{
stackItem = item;
stackSize = 1;
return true;
}
if (item.itemID != stackItem.itemID || stackSize >= GetMaxStackSize()) return false;
stackSize++;
return true;
}

public int GetStackSize()
{
return stackSize;
}

public int GetMaxStackSize()
{
if(stackItem == null)
{
return -1;
}
else
{
return stackItem.itemMaxStackSize;
}
}

public bool HasFreeSpace()
{
if (stackSize < GetMaxStackSize()) return true;
return false;
}
}
That’s the ItemStack
so now if a call the Add(Item item) method in the inventory class the stack is being added to the array but the variable stackItem which is assigned in the constructor ItemStack(Item item, int amount) gets null

Wait a second, so… Your array is of ItemStack objects. And you actually create it and add it to your inventory. Although that doesn’t mean that that stack’s item variable has a not null object. You may be creating a ItemStack with a null object.

If I were you I’d add a

if (item == null) {
    Debug.Log("Null Item");
    return false;
}

inside your method and check what happens.