[英]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? Null指针通常意味着在某个地方存在程序员错误,是否可以查看我的代码以确保我没有错过任何明显的事情?
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: 这是添加的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;
}
The Action method inherits from the interface class Client.java: Action方法从接口类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. 当我使用任何两个不同的漫游器时,它运行正常,没有错误。
不清楚你在哪里得到NPE,但我敢打赌null输入参数
Set<Action> actions
A NullPointerException
, may as well happen if you try to invoke a method, on a null
variable. 如果尝试在null
变量上调用方法,也可能发生NullPointerException
。 That's possibly the case here. 这里可能就是这种情况。
If this doen't help, take your time to post the exact line that throws the exception. 如果这样做没有帮助,请花一些时间发布引发异常的确切行。
Can you check if(null == actions)
? 你可以检查if(null == actions)
吗? This would provide some insight. 这将提供一些见解。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.