gpt4 book ai didi

Java HashMap 返回 null

转载 作者:行者123 更新时间:2023-12-01 17:34:12 24 4
gpt4 key购买 nike

我得到的东西返回为空,一切总是设置好的,并且它在一定程度上有效......

我不明白为什么会遇到这个问题...请帮忙,

一切都应该设置好,但我得到的 block 返回为空

package com.blazingkin.world;
import java.util.HashMap;
import java.util.Map;

import com.blazingkin.atrox.AtroxAdventrum;

public class World {

public World(AtroxAdventrum aa){
chunks = new HashMap<Integer,Map<Integer,Chunk>>();
}



public void setBlock(int x, int y, int newBlock){

}

public void setMetadata(int x, int y, int newMeta){

}

private void setChunk(int x, int y, Chunk c) {
if(chunks.get(x) == null) {
chunks.put(x, new HashMap<Integer, Chunk>());
System.out.println("Created new chunk "+ x);
}
chunks.get(x).put(y, c);
System.out.println("Set Chunk: "+x+", "+y);
}
private Chunk getChunk(int x, int y) {
if(chunks.get(x) == null){
Chunk c = new Chunk(y*64, x, y);
setChunk(x, y, c);
return c;
}
return chunks.get(x).get(y);
}


public int getChunkX(int x){
return x/64 - x%64;
}
public int getChunkY(int y){
return y/64 - y%64;
}

public int getBlock(int x, int y){
return getChunk(getChunkX(x), getChunkY(y)).getBlock(x&64, y%64);
}

public int getMetadata(int x, int y){
return 1;
}



Map<Integer,Map<Integer,Chunk>> chunks;

}

错误消息:

...

Created new chunk -23

Set Chunk: -23, 16


Exception in thread "main" java.lang.NullPointerException
at com.blazingkin.world.World.getBlock(World.java:49)
at com.blazingkin.render.ScreenOutput.render(ScreenOutput.java:30)
at com.blazingkin.atrox.AtroxAdventrum.draw(AtroxAdventrum.java:34)
at com.blazingkin.atrox.Core.gameLoop(Core.java:60)
at com.blazingkin.atrox.Core.run(Core.java:27)
at com.blazingkin.atrox.AtroxAdventrum.main(AtroxAdventrum.java:14)

最佳答案

您有一个检查可以执行此操作:if(chunks.get(x) == null){

但是你永远不会检查chunks.get(x).get(y)以及get(y)是否为空! :-O

关于Java HashMap 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8030645/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com