我实际上遇到了三个java.langNullPointerException错误,但是其中两个是相同的,所以如果我修复了一个,我会修复另一个错误。
我正在尝试制作一个Game Class来传递gameTest
以下是我的游戏类的代码
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 }
答案 0 :(得分:1)
您没有在player1
()`
instance.getPlayer
//79 public void testSetPlayer1() {
//80 System.out.println("setPlayer1");
//81 Game instance = new Game();
//82 Player p1 = new Player("Jim");
instance.setPlayer1(p1);
//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());
}
在这两种情况下你都有同样的问题。
//51 public void testNoArgConstructor() {
//52 System.out.println("Game");
//53 Game instance = new Game();
instance.setPlayer1(new Player("Player One");
//54 assertEquals("?", instance.getPlayer1().getName());
instance.setPlayer2(new Player("Player Two");
//55 assertEquals("?", instance.getPlayer2().getName());
//56 assertEquals(true, instance.isPlayer1Deals());
//57 }
如果您无法编辑测试用例,可以尝试Game
public Game() {
player1 = new Player("?");
player1 = new Player("?");
player1Deals = true;
}
答案 1 :(得分:0)
您已经创建了一个游戏实例但是您从未实例化任何玩家,所以当您使用getPlayer1()
时,没有玩家。
在创建游戏后但在引用instanse.setPlayer1(new Player());
之前调用getPlayer()
。
答案 2 :(得分:0)
沿着桌子走一小段路,看看......
Game instance = new Game();
// instance.player1 and instance.player2 are null...
Player p1 = new Player("Jim");
p1.setHand(hand1);
Player p2 = new Player("George");
p2.setHand(hand2);
// instance.player1 and instance.player2 are still null...
// Break here...
assertEquals("?", instance.getPlayer1().getName());
instance.setPlayer1(p1);
如果您要将代码更改为更像......
Game instance = new Game();
Player p1 = new Player("Jim");
p1.setHand(hand1);
Player p2 = new Player("George");
p2.setHand(hand2);
instance.setPlayer1(p1);
assertEquals("?", instance.getPlayer1().getName());
你可能会有更多的成功......
这就是......
Game instance = new Game();
assertEquals("?", instance.getPlayer1().getName());
assertEquals("?", instance.getPlayer2().getName());
assertEquals(true, instance.isPlayer1Deals());
是同一问题的症状,instance.player1
和instance.player2
都是null
我“认为”你的困惑正在冒出来......
public Game() {
player1 = player1;
player1 = player2; // Not sure if this is correct...
player1Deals = true;
}
什么都不做什么都没有......
这实际上与说...
相同public Game() {
player1 = null;
player1 = null;
player1Deals = true;
}
答案 3 :(得分:0)
我认为您的getPlayer1()
功能属于Player
类型,因此您应该使用p1.getPlayer1()
。您instance
是Game()
类型。而且,我认为您的功能getName()
应定义为Player
类型。