0

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.

Here's my little bit of code:

public void buttonPressed(int buttonID) {
    if(buttonID < 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;
            }
        }
    }
}

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.

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.

6
  • To me it looks like this code should work just fine - how did you check and find out that the field was not set to null? Commented Feb 24, 2016 at 1:33
  • Have you tried stepping through it with a debugger? I think it's unlikely that it's running that statement but if it is some other code must be setting it back to non-null. Commented Feb 24, 2016 at 1:34
  • What kind of type is slots[]? Commented Feb 24, 2016 at 1:40
  • Terminology problems here. There is no such thing as setting an object to null. You can set variables to null. Commented Feb 24, 2016 at 1:44
  • To answer all of your questions: slots[] is an array with a size of four of the type 'Slot', I know because of a null-safe print function I have and just using standard print, and I haven't tried stepping through yet. I'll give that a shot asap. Commented Feb 24, 2016 at 1:44

1 Answer 1

2

You don't need most of that if structure, the swap will work whether the values are null or not. Lets assume 0 was passed as the buttonID, there is an item stored in inHand, but no item in slot 0.

public void buttonPressed(int buttonID) {
    if(buttonID < slots.length) {
        //The item in inHand is now placed into swapSpot
        Item swapSpot = inHand;
        //The null value in slots[buttonID].storedItem is now placed in inHand
        inHand = slots[buttonID].storedItem;
        //The item previously in inHand is now placed in slots[buttonID].storedItem
        slots[buttonID].storedItem = swapSpot;
    }
}

I'm not sure why your code doesn't work right, it looks like it should work, but obviously there's something wrong that isn't visible at first glance. Try simplifying it like this. Code which is less verbose tends to be less error prone as it is easier to manage the logic.

Sign up to request clarification or add additional context in comments.

2 Comments

I never realized that. I guess that's what happens when you try to implement something as fast as you can. Thanks a lot, still trying to trace my error. From what I understand, inHand is being reset from somewhere else, but I'm confused because I only reference it a few times. Oh well, thanks a lot!
@CarlLitchman As stated by another user, I would recommend stepping through with a debugger. Place breakpoints any time the variable is set, watch the value of the variable, and any value being placed into it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.