尝试运行此程序时出现错误。这不是整个程序,只是我认为问题所在的几个细节。错误指出
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: public boolean isEmpty(){ for(int i = 0; i < board.length; i++) { if (board[i].isEmpty()) return true; } 返回假;}
我的编码可能完全错误。我还在学习中,请耐心等待。谢谢!