嘗試運行此程序時出現錯誤。這不是整個程序,只是我認爲問題出在哪裏的幾個細節。錯誤指出線程「main」中的異常java.lang.NullPointerException?這是什麼意思?
if(board[randSpace].isEmpty()) and
cl.makeChutes(5);"
錯誤是:
"Exception in thread "main" java.lang.NullPointerException
at ChutesAndLadders.makeChutes(ChutesAndLadders.java:29)
at ChutesAndLadders.main(ChutesAndLadders.java:67)"
這是我的計劃:
import java.util.Random;
public class ChutesAndLadders{
Cell [] board = new Cell [100];
Random rand = new Random();
public int chutes, ladders;
public void makeChutes (int a){
int makeChutes = a;
for (int i = 0; i < a; i++){
int randSpace = rand.nextInt(99);
if(board[randSpace].isEmpty())
board[randSpace] = new Cell (-10,"C");
else
i--;
}
}
public static void main(String[] args) {
// Create an instance of ChutesAndLadders
ChutesAndLadders cl = new ChutesAndLadders(10,10);
// Randomly place 5 more chutes and 5 more ladders
cl.makeChutes(5);
cl.makeLadders(5);
這裏是我的isEmpty: 公共布爾的isEmpty(){ 爲(INT I = 0; i < board.length; i ++) if(board [i] .isEmpty()) 返回true; } return false; }
我的編碼可能完全錯誤。我仍然在學習,所以請耐心等待。謝謝!