/ / Java HashMap vracia null - java, 2d

Java HashMap vracia null - java, 2d

Dostávam veci vrátené ako null, všetko je vždy nastavené a funguje to až do bodu ...

I dont pochopiť, prečo som "m dostať tento problém ... prosím, pomôžte,

Všetko by malo byť nastavené, ale ja dostanem kus vrátený ako null

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;

}

Chybná správa:

...

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)

odpovede:

5 pre odpoveď č. 1

Máte kontrolu, ktorá to robí: if(chunks.get(x) == null){

Ale nikdy sa nepozeráš chunks.get(x).get(y) a ak get(y) je null! : -O