(Board.java :23) at PiratesDriver. main(PiratesDriver.java:14)-6ren"> (Board.java :23) at PiratesDriver. main(PiratesDriver.java:14)-这个问题不太可能对任何 future 的访客有帮助;它只与一个较小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,通常不适用于全世界的互联网受众。如需帮助使此问题更广泛适用,visit the -6ren">
gpt4 book ai didi

java - Board 线程 "main"java.lang.NullPointerException 中出现异常。 at(Board.java :23) at PiratesDriver. main(PiratesDriver.java:14)

转载 作者:行者123 更新时间:2023-12-01 04:48:13 26 4
gpt4 key购买 nike

您好,我收到此错误消息,我需要帮助

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 为空或其他什么地方,但我似乎无法修复它,我猜我必须在 SetDifficulty 中修复它() 因为这就是 diffLevel 定义的地方。请需要帮助。哦,第 14 行是 private int diffLevel;

最佳答案

players[0] = new Player();
players[1] = new Player();

开始然后使用它。请记住:当您创建数组的新实例时,您将在单元格中使用 null(默认)值创建它。在使用它之前,您必须创建对象的实例。

另请参阅:

关于java - Board 线程 "main"java.lang.NullPointerException 中出现异常。 at<init>(Board.java :23) at PiratesDriver. main(PiratesDriver.java:14),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15467197/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com