Java - Setting an object to null
<p>I've seen that there are questions similar to this out there, but they seem rather niche to actually deleting the object reference and whatnot. I'm working on an inventory system for a game I'm working on, and rely on item pickup/swap/put-down based on wether a slot contains a null object or not.</p>

<p>Here's my little bit of code:</p>

<pre><code>public void buttonPressed(int buttonID) {
    if(buttonID &lt; slots.length) {
        if(inHand == null) {
            if(slots[buttonID].storedItem != null) {
                inHand = slots[buttonID].storedItem;
                slots[buttonID].storedItem = null;
            }
        } else {
            if(slots[buttonID].storedItem == null) {
                slots[buttonID].storedItem = inHand;
                inHand = null;
            } else {
                Item swapSpot = inHand;
                inHand = slots[buttonID].storedItem;
                slots[buttonID].storedItem = swapSpot;
            }
        }
    }
}
</code></pre>

<p>The checks are working correctly, but when the code in the first if statement is run (slots[buttonID].storedItem != null), the Object 'storedItem' in the specified slot from the array is not being set to null. I sincerely apologize if there's something already out there, but I can't wrap my head around what people out there are saying.</p>

<p>Edit: I fixed it - nothing was wrong with the code I shared, but there was an issue with the implementation of my MouseListener. Long story short, it was being double-registered, and as soon as you picked up an item, it would be put back down instantly.</p>