简体   繁体   English

Java-将对象设置为null

[英]Java - Setting an object to null

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. 这些检查工作正常,但是当运行第一个if语句中的代码(slots [buttonID] .storedItem!= null)时,未将数组中指定插槽中的对象'storedItem'设置为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. 编辑:我已修复它-我共享的代码没有问题,但是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. 长话短说,它已被双重注册,一旦您拿起一个项目,它就会立即放回原处。

You don't need most of that if structure, the swap will work whether the values are null or not. 您无需使用大多数if结构,无论值是否为null,交换都将起作用。 Lets assume 0 was passed as the buttonID, there is an item stored in inHand, but no item in slot 0. 假设传递了0作为buttonID,inHand中存储了一个项目,但是插槽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. 较不冗长的代码往往不易出错,因为它更易于管理逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM