我实际上得到了三个 java.langNullPointerException 错误,但其中两个是相同的,所以如果我修复一个,我将修复另一个。
我正在尝试制作一个游戏类来通过游戏测试
这是我的游戏类的代码
public class Game implements CribbageConstants {
private Player player1, player2;
private boolean player1Deals;
private Game game;
/**
* No argument constructor - set default values for Game
*/
public Game() {
player1 = player1;
player1 = player2;
player1Deals = true;
}
// convenience constructor
public Game(Player player1, Player player2) {
this.player1 = player1;
this.player2 = player2;
this.player1Deals = true;
}
//mutator
public void setPlayer1(Player player1) {
this.player1 = player1;
}
public void setPlayer2(Player player2) {
this.player2 = player2;
}
public void setPlayer1Deals(boolean player1Deals) {
this.player1Deals = player1Deals;
}
public void setGame(Game game) {
this.game = game;
}
//utility methods
public Player getPlayer1() {
return player1;
}
public Player getPlayer2() {
return player2;
}
/**
* @return the player1Deals
*/
public boolean isPlayer1Deals() {
return player1Deals;
}
/**
* @return the game
*/
public Game getGame() {
return game;
}
}
我在以下代码的第 86 行收到了一个 NullPointerException:
//79 public void testSetPlayer1() {
//80 System.out.println("setPlayer1");
//81 Game instance = new Game();
//82 Player p1 = new Player("Jim");
//83 p1.setHand(hand1);
//84 Player p2 = new Player("George");
//85 p2.setHand(hand2);
//86 assertEquals("?", instance.getPlayer1().getName());
//87 instance.setPlayer1(p1);
//88 assertEquals("Jim", instance.getPlayer1().getName());
}
我在 testNoArgConstructor 上收到了一个 NullPointerException(第 54 行)
//51 public void testNoArgConstructor() {
//52 System.out.println("Game");
//53 Game instance = new Game();
//54 assertEquals("?", instance.getPlayer1().getName());
//55 assertEquals("?", instance.getPlayer2().getName());
//56 assertEquals(true, instance.isPlayer1Deals());
//57 }