嗨,我收到此错误消息,我需要帮助
Exception in thread "main" java.lang.NullPointerException
at Board.<init>(Board.java:23)
at PiratesDriver.main(PiratesDriver.java:14)
我的代码是
import java.util.Scanner;
import java.util.Random;
public class Board {
private final int SIZE=30;
private final int NUM_PLAYERS=2;
**private int diffLevel;**
private Player[] players= new Player[NUM_PLAYERS];
private char[] hurdles=new char[SIZE];
public Board() {
System.out.println("|||||||||||||||||||||||||||||||||||||||||||||"+"\n||||||||||||||||||||START||||||||||||||||||||\n"+"|||||||||||||||||||||||||||||||||||||||||||||" );
System.out.print("Enter Diffculty Level for the Game (0=easy, 1=50/50 i.e. not too easy, not too hard, 2=HARD):");
**SetDifficulty();**
System.out.println("");
players[0].SetName(GetPlayerName(0));
System.out.println("");
players[1].SetName(GetPlayerName(1));
DisplayPlayerDetails();
System.out.println("");
System.out.println("<<<--GAME BOARD LEGEND-->>>");
System.out.println("e = This cell contains an easy enemy pirate.");
System.out.println("t = This cell contains a treasure island.");
System.out.println("a = This cell contains an abandoned ship.");
System.out.println("m = This cell contains a mystery island.");
System.out.println("j = This cell contains a magic chamber.");
System.out.println(",P = This cell has a player in it.");
System.out.println("<<<------- ----- ------->>> ");
System.out.println("");
SetHurdles();
DisplayBoard();
}
**void SetDifficulty(){
Scanner scan= new Scanner(System.in);
boolean invalid = true;
diffLevel= scan.nextInt();
while (!((diffLevel <=2) && (diffLevel>=0))){
System.out.println("Error, please enter a valid value");
diffLevel= scan.nextInt();**
}
}
public String GetPlayerName(int index){
Scanner scan= new Scanner(System.in);
System.out.print("Enter Player "+(index+1)+"'s Name: ");
String playername= scan.nextLine();
System.out.println(playername+" has no Voodoo doll.");
System.out.println(playername+" does not have a Ship.");
return playername;
}
public void DisplayPlayerDetails(){
System.out.println("------------- PLAYER DETAILS -------------");
for (int i=0;i<players.length;i++){
System.out.println("-->Player "+(i+1)+"<--");
System.out.println("Name: "+players[i].GetName());
System.out.println("Health: "+players[i].GetHealth()+" potions");
System.out.println("Treasure: "+players[i].GetWealth()+" gold coins");
System.out.println("Weapon: "+players[i].WeaponName(players[i].GetWeapon()));
System.out.println("Voodoo Doll: "+players[i].GetVoodoo());
System.out.println("Ship: "+players[i].GetShip());
System.out.println("Position: "+(players[i].GetPosition()+1));
System.out.println(" ------><------");
}
System.out.println("---------------------------------------------");
}
public void SetHurdles(){
Random randNumGenerator = new Random();
boolean bool=true;
int rand;
int e,h,t,a,m,j;
if (diffLevel==0){
e=12;
h=0;
}
else if (diffLevel==1){
e=6;
h=6;
}
else {
h=12;
e=0;
}
t=6;
a=6;
m=3;
j=3;
for (int i=0;i<hurdles.length;i++){
while(bool){
rand=randNumGenerator.nextInt(6);
if (rand==0&& e!=0){
hurdles[i]='e';
e--;
bool=false;
}
else if (rand==1&& h!=0){
hurdles[i]='h';
h--;
bool=false;
}
else if (rand==2&& t!=0){
hurdles[i]='t';
t--;
bool=false;
}
else if (rand==3&& a!=0){
hurdles[i]='a';
a--;
bool=false;
}
else if (rand==4&& m!=0){
hurdles[i]='m';
m--;
bool=false;
}
else if (rand==5&& j!=0){
hurdles[i]='j';
j--;
bool=false;
}
bool=true;
}
}
}
public void DisplayBoard(){
System.out.println("---------------------------------------------");
System.out.println("********PIRATES OF 125 GAME BOARD********");
for (int i=0;i<5;i++){
if ((players[0].GetPosition()==i)||(players[1].GetPosition()==i))
System.out.print((i+1)+": "+hurdles[i]+"P");
else
System.out.print((i+1)+": "+hurdles[i]);
}
System.out.println("");
for (int i=5;i<10;i++){
if ((players[0].GetPosition()==i)||(players[1].GetPosition()==i))
System.out.print((i+1)+": "+hurdles[i]+"P");
else
System.out.print((i+1)+": "+hurdles[i]);
}
System.out.println("");
for (int i=10;i<15;i++){
if ((players[0].GetPosition()==i)||(players[1].GetPosition()==i))
System.out.print((i+1)+": "+hurdles[i]+"P");
else
System.out.print((i+1)+": "+hurdles[i]);
}
System.out.println("");
for (int i=15;i<20;i++){
if ((players[0].GetPosition()==i)||(players[1].GetPosition()==i))
System.out.print((i+1)+": "+hurdles[i]+"P");
else
System.out.print((i+1)+": "+hurdles[i]);
}
System.out.println("");
for (int i=20;i<25;i++){
if ((players[0].GetPosition()==i)||(players[1].GetPosition()==i))
System.out.print((i+1)+": "+hurdles[i]+"P");
else
System.out.print((i+1)+": "+hurdles[i]);
}
System.out.println("");
for (int i=25;i<30;i++){
if ((players[0].GetPosition()==i)||(players[1].GetPosition()==i))
System.out.print((i+1)+": "+hurdles[i]+"P");
else
System.out.print((i+1)+": "+hurdles[i]);
}
System.out.println("");
System.out.println("---------------------------------------------");
}
public void Play(){
Board game=new Board();
for (int i=0; i<players.length;i++){
DisplayPlayerDetails();
while(players[i].GetPosition()<hurdles.length){
players[i].Move();
if (hurdles[players[i].GetPosition()]=='e'){
EasyPirate e= new EasyPirate();
}
}
}
}
}
我的主要动力是
public class PiratesDriver {
public static void main(String[] args) {
Board piratesOf125 = new Board(); //Instantiate a new board
piratesOf125.Play(); //Play the game
}
}
我已经尝试了一个多小时了,我知道问题出在我的 diffLevel 为 null 之类的问题上,但我似乎无法修复它,我猜我必须在 SetDifficulty() 中修复它这就是 diffLevel 定义的地方。需要帮助。哦,第 14 行是私有的 int diffLevel;