空指针通常意味着在某个地方会有程序员错误,是否可以查看我的代码以确保我没有错过任何明显的事情?

这只是一个简单的基于%的扑克机器人,很确定这是错误的“思考”所在。

public Action act(Set<Action> actions) {
    Random generator = new Random();
    int roll = generator.nextInt(100) + 1; //gives number between 1 and 100
    System.out.println("roll = " + roll);
    Action myAction = null;

    if (roll <= 30) { // RAISE 30%
        if (actions.contains(Action.RAISE)) {
            myAction = Action.RAISE;
        } else if (actions.contains(Action.BET)) {
            myAction = Action.BET;
        } else if (actions.contains(Action.CALL)) {
            myAction = Action.CALL;
        }
    } else if (roll > 30 && roll <= 90) { // CALL/CHECK 60%
        if (actions.contains(Action.CALL)) {
            myAction = Action.CALL;
        } else if (actions.contains(Action.CHECK)) {
            myAction = Action.CHECK;
        }
    } else if (roll > 90) { // FOLD 10%
        if (actions.contains(Action.FOLD)) {
            myAction = Action.FOLD;
        }

    return myAction;
}


}

编辑:

这是添加的Set Action方法:

public Action act(Set<Action> actions, int minBet, int currentBet) {
    action = client.act(actions);
    switch (action) {
        case CHECK:
            break;
        case CALL:
            betIncrement = currentBet - bet;
            if (betIncrement > cash) {
                //TODO: All-in with partial Call.
                betIncrement = cash;
            }
            cash -= betIncrement;
            bet += betIncrement;
            break;
        case BET:
            betIncrement = minBet;
            if (betIncrement >= cash) {
                //TODO: All-in with partial Bet.
                betIncrement = cash;
            }
            cash -= betIncrement;
            bet += betIncrement;
            raises++;
            break;
        case RAISE:
            currentBet += minBet;
            betIncrement = currentBet - bet;
            cash -= betIncrement;
            bet += betIncrement;
            raises++;
            break;
        case FOLD:
            hand.removeAllCards();
            break;
    }
    return action;
}


Action方法从接口类Client.java继承:

Action act(Set<Action> allowedActions);


非常感谢!

解:

当我尝试对自己运行两个相同的机器人时,它在某处发生冲突,导致空指针。当我使用任何两个不同的漫游器时,它运行正常,没有错误。

最佳答案

不清楚你在哪里得到NPE,但我敢打赌null输入参数

 Set<Action> actions

10-07 15:42