您好,感謝您閱讀此。我在java/LWJGL中編寫代碼。我的問題是,如果我不包含這一點,我會不斷收到一些代碼的空指針錯誤。基本上有4類的Java空指針錯誤
- 一個塊類。
- 一個blockGrid類
- 一個blocktype類
- 以及引導類。
boot類創建一個顯示,並在遊戲循環中運行blockgrid類中的draw方法。要設置塊的位置,我將使用blockgrid方法內的renderat(x,y)方法。塊類只是在某個x,y上創建一個四元組。
很抱歉,如果我沒有解釋好。這是我的代碼:這是發生錯誤的地方,只是閱讀我的評論,看看錯誤在哪裏。
// BlockGrid.java
package minecraft2d;
import java.io.File;
public class BlockGrid {
private Block[][] blocks = new Block[100][100];
public BlockGrid() {
for (int x = 0; x < 25 - 1; x++) {
for (int y = 0; y < 16 - 1; y++) {
blocks[x][y] = new Block(BlockType.AIR, -100, -100); //This is where my error happens! If I don't include this line i get a null pointer. Anything will help. I am really stuck and don't know whats happening
}
}
}
public void setAt(int x, int y, BlockType b) {
blocks[x][y] = new Block(b, x * 32, y * 32);
}
public void draw() {
for (int x = 0; x < 25 - 1; x++) {
for (int y = 0; y < 16 - 1; y++) {
blocks[x][y].draw();
}
}
}
}
有什麼錯誤訊息?例如,堆棧跟蹤會有所幫助。 – 2012-04-20 21:24:49
Block類的構造函數是什麼樣的? – JavaKungFu 2012-04-20 21:25:44