我在介绍CS类,我正在编写一个程序,玩游戏。我有框架类,但当我尝试运行时发生此错误。它说在游戏方法的第51行有一个零点,但我找不到零点出现的地方。这是四个类的代码。无法找到哪里[线程中的异常“main”java.lang.NullPointerException]应用
public class Pog //THIS IS DONE
{
public static void main (String [] Agrs)
{
PogPlayer human = new PogPlayer("Human");
PogPlayer computer = new PogPlayer("Computer");
PogGame game = new PogGame (human, computer);
game.play();
} // method main
} // class Pog
public class PogDice
{
private int die1; // Stores the value (1-6) of die1.
private int die2; // Stores the value (1-6) of die2.
private Random rand;
public PogDice()
{
rand = new Random();
} // default constructor
public int roll()
{
int total = 0;
int turnTotal = 0;
String choice;
Scanner scan;
scan = new Scanner(System.in);
do { die1 = rand.nextInt(5)+1;
die2 = rand.nextInt(5)+1;
total = die1+die2;
System.out.println("Current score is: " + total + "\nRoll again?");
choice = scan.nextLine();
} while (choice == "yes" && hasDoubleOne() == false);
turnTotal = turnTotal + total;
return turnTotal;
} // method rollDice
public boolean hasDoubleOne()
{
boolean doubleOne = false;
if(die1 == 1 && die2 == 1)
doubleOne = true;
else
doubleOne = false;
return doubleOne;
} // method hasDoubleOne
public String toString()
{
return (die1 + ", " + die2);
} // method toString
} // class PogDice
public class PogGame
{
private PogPlayer human;
private PogPlayer computer;
/**
* PogGame (constructor)
*
* @param PogPlayer (human)
* @param PogPlayer (computer)
*/
public PogGame (PogPlayer humanArg, PogPlayer computerArg)
{
PogPlayer human = humanArg;
PogPlayer comptuer = computerArg;
} // method PogGame (constructor)
public void play()
{
System.out.println("Hello, Welcome to Pog.\n\n This game takes the user "+
"and puts them agaisnt the computer\n in a dice-based "+
"game. Each player takes turns rolling two dice,\n "+
"with their sum being the total points the "+
"player gets.\n If you roll two 1s you lose all "+
"points that turn.\n You have the option to turn "+
"over the dice at any point\n during your turn to "+
"keep all of your points.\n First one to 21 wins!.\n");
human.getCurrent();
} // method play
} // class pogGame
public class PogPlayer
{
private int current; // Value of the current roll
private int score; // Player's point score.
private PogDice dice; // Player's dice
private String name; // Player's name.
public final int WINNER = 21; // number of points required to win
public PogPlayer (String nameArg)
{
PogDice dice = new PogDice();
score = 0;
} // method PogPlayer (constructor)
public int getCurrent()
{
int current;
current = dice.roll();
return current;
} // method getCurrent
public PogDice getDice()
{
new PogDice();
return new PogDice();
} // method getDice
public int getScore()
{
score = current + score;
return score;
} // method getScore
public boolean hasWon()
{
boolean win;
if(score == WINNER)
{
win = true;
}
else
win = false;
return win;
} // method hasWon
public boolean rollDice()
{
return false;
} // method rollDice
} // class PogPlayer
我很确定错误发生在human.getCurrent();线。
哪些原因会导致该行一NPE? –
我能知道第51行是什么吗? – djechlin
你的堆栈跟踪在哪里? – kolossus