0

我实际上得到了三个 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   }
4

4 回答 4

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;



    }
于 2013-10-04T03:07:53.097 回答
0

我认为你的getPlayer1()函数是一种Player类型,所以你应该使用p1.getPlayer1(). 你instance是一个Game()类型。而且,我认为你的函数getName()应该定义为一种Player类型。

于 2013-10-04T03:26:45.313 回答
0

您已经创建了一个 Game 实例,但您从未实例化任何玩家,因此当您使用 时getPlayer1(),没有玩家。

所以instanse.setPlayer1(new Player());在你创建游戏之后但在你引用getPlayer().

于 2013-10-04T03:08:04.180 回答
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;
}
于 2013-10-04T03:08:32.690 回答