silverzx
silverzx

Reputation: 1249

Why does this throw a NullPointerException?

Null pointer usually means there is a programmers error within a paramter somwhere, could my code be looked at to ensure I haven't missed anything obvious?

It's just a simple % based poker bot, pretty sure here's where it "THINKS" the error is.

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;
}

}

EDIT:

Heres the added set Action method:

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;
}

The Action method inherits from the interface class Client.java:

Action act(Set<Action> allowedActions);

Many thanks!

SOLUTION:

When I try and run two of the same bot against itself it has a conflict somewhere causing the null pointer. When I use any two different bots it plays fine with no errors.

Upvotes: 1

Views: 145

Answers (3)

kuriouscoder
kuriouscoder

Reputation: 5582

Can you check if(null == actions)? This would provide some insight.

Upvotes: 0

Mordechai
Mordechai

Reputation: 16234

A NullPointerException, may as well happen if you try to invoke a method, on a null variable. That's possibly the case here.

If this doen't help, take your time to post the exact line that throws the exception.

Upvotes: 0

Martin V.
Martin V.

Reputation: 3710

not clear where are you getting NPE but i would bet on null input parameter

 Set<Action> actions

Upvotes: 2

Related Questions