このプログラムを実行しようとするとエラーが発生します。これはプログラム全体ではなく、問題がどこにあるかを私が考えるほんの少しの詳細です。エラーが指摘する
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です。publicbooleanisEmpty(){for(int i = 0; i <board.length; i ++){if(board [i] .isEmpty())return true; }falseを返します。}
コーディングが完全に間違っている可能性があります。私はまだ学んでいるので、しばらくお待ちください。ありがとうございました!