宣布:
private Man[] man;
这是初始化:
Man[] man = new Man[1];
for (int i = 0; i < 1; i++){
man[i] = new Man();
for (int j = 0; j < 3; j++){
man[i].eatThis(table.foods[table.topFood-1]);
table.topFood--;
}
}
想打印这个:
System.out.println(getMan(0));
去:
public Man getMan(int k){
return man[k];
}
但我收到 NullPointerException。为什么?尽管:
System.out.println(man[0]);
工作得很好。
它返回:
Man = (bread, meat, cheese)
这是一个例外:
Exception in thread "main" java.lang.NullPointerException
at ManRunning.getMan(ManRunning.java:80)
at ManRunning.newGame(ManRunning.java:133)
at ManRunning.<init>(ManRunning.java:57)
at RunDevilRun.main(RunDevilRun.java:9)