[英]Java Exception: Default constructor needed? There is already one
I am writing code to try to create a poker playing AI. 我正在编写代码以尝试创建玩AI的扑克。 I have been provided with .jar files containing classes that will handle playing the game, as long as I get the AI code working. 只要提供了AI代码,我就会获得.jar文件,这些文件包含可以处理游戏的类。
With that said, I am encountering the following rather annoying error. 话虽如此,我遇到了以下相当烦人的错误。 For clarity, the following code block contains the error, the section of code that calls my player in PokerGame.class
, the PokerPlayer
interface, and my class that extends the PokerPlayer
interface. 为了清楚起见,以下代码块包含错误,在PokerGame.class
中调用我的玩家的部分代码, PokerPlayer
接口以及扩展PokerPlayer
接口的我的类。
This is the exception: 这是例外:
Exception in thread "main" java.lang.RuntimeException: Class blake.MyPokerPlayer must have a default constructor
at pokergame.PokerGame.loadPlayer(PokerGame.java:84)
at pokergame.PokerGame.<init>(PokerGame.java:100)
at pokergame.PokerGame.main(PokerGame.java:590)
at blake.MyPokerPlayer.main(MyPokerPlayer.java:18)
Caused by: java.lang.NoSuchMethodException: blake.MyPokerPlayer.<init>()
at java.lang.Class.getConstructor0(Class.java:3082)
at java.lang.Class.getConstructor(Class.java:1825)
at pokergame.PokerGame.loadPlayer(PokerGame.java:78)
... 3 more
And this is the code: 这是代码:
package pokergame;
import java.util.*;
import java.lang.reflect.*;
public class PokerGame implements PokerListener {
// Other methods above
private PokerPlayer loadPlayer(String classname) {
PokerPlayer ret = null;
try {
Class<?> cls = Class.forName(classname);
Constructor<?> ctor = cls.getConstructor();
Object o = ctor.newInstance();
ret = (PokerPlayer)o;
} catch (ClassNotFoundException e) {
throw new RuntimeException("Cannot find player class: " + classname, e);
} catch (NoSuchMethodException e) {
throw new RuntimeException("Class " + classname + " must have a default constructor", e);
} catch (ClassCastException e) {
throw new RuntimeException("Player class does not implement PokerPlayer interface: " + classname, e);
} catch (Exception e) {
throw new RuntimeException("Error trying to instantiate player class: " + classname, e);
}
return ret;
}
// Other methods below
}
package pokergame;
public interface PokerPlayer extends PokerListener {
int numChips();
void collectChips(int numChips);
void acceptChips(int numChips);
void setId(String id);
PokerDecision decide(PokerGameDetails game, int betRequiredToCall);
PokerHand bestHand();
}
package player;
import java.util.*;
import pokergame.*;
public class MyPokerPlayer implements PokerPlayer {
private String id;
private ArrayList<PokerCard> handCards = new ArrayList<>();
private ArrayList<PokerCard> tableCards = new ArrayList<>();
private ArrayList<PokerCard> deck = new ArrayList<>();
private int numChips;
public static void main(String[] args) {
PokerGame.main(new String[]{"pokerstooges.Larry", "player.MyPokerPlayer"});
}
MyPokerPlayer() { }
// ***************
// THESE NEED WORK
// ***************
public void onEvent(PokerEvent e) {
if (e instanceof PokerNewHandEvent) { System.out.println(e); }
else if (e instanceof PokerPlayerEliminatedEvent) { System.out.println(e); }
else if (e instanceof PokerPotDistributionEvent) { System.out.println(e); }
else if (e instanceof PokerCollectingBlindEvent) { System.out.println(e); }
else if (e instanceof PokerNewCardEvent) { System.out.println(e); }
else if (e instanceof PokerDecisionEvent) { System.out.println(e); }
else if (e instanceof PokerHandRevealEvent) { System.out.println(e); }
}
public PokerDecision decide(PokerGameDetails game, int betRequiredToCall) {
PokerDecision decision = new PokerDecision(PokerDecision.TYPE.FOLD);
return decision;
}
public String toString() {
// Something here...
return "";
}
// *****************
// THESE SHOULD WORK
// *****************
public int numChips() { return this.numChips; }
public void collectChips(int numChips) { this.numChips -= numChips; }
public void acceptChips(int numChips) { this.numChips += numChips; }
public void setId(String id) { this.id = id; }
public String getId() { return this.id; }
public PokerHand bestHand() {
ArrayList<String> hands = new ArrayList<>();
String handCardString = handCards.get(0) + " " + handCards.get(1);
if (tableCards.size() == 0) {
hands.add(handCardString);
}
else if (tableCards.size() == 3) {
hands.add(handCardString + " " + tableCards.get(0) + " " + tableCards.get(1) + " " + tableCards.get(2));
}
else if (tableCards.size() > 3) {
hands.add(handCardString + " " + tableCards.get(0) + " " + tableCards.get(1) + " " + tableCards.get(2));
hands.add(handCardString + " " + tableCards.get(0) + " " + tableCards.get(1) + " " + tableCards.get(3));
hands.add(handCardString + " " + tableCards.get(0) + " " + tableCards.get(2) + " " + tableCards.get(3));
hands.add(handCardString + " " + tableCards.get(1) + " " + tableCards.get(2) + " " + tableCards.get(3));
if (tableCards.size() == 5) {
hands.add(handCardString + " " + tableCards.get(0) + " " + tableCards.get(1) + " " + tableCards.get(4));
hands.add(handCardString + " " + tableCards.get(0) + " " + tableCards.get(2) + " " + tableCards.get(4));
hands.add(handCardString + " " + tableCards.get(0) + " " + tableCards.get(3) + " " + tableCards.get(4));
hands.add(handCardString + " " + tableCards.get(1) + " " + tableCards.get(2) + " " + tableCards.get(4));
hands.add(handCardString + " " + tableCards.get(2) + " " + tableCards.get(3) + " " + tableCards.get(4));
}
}
PokerHand bestHand = compareHands(hands);
return bestHand;
}
public PokerHand compareHands(ArrayList<String> hands) {
PokerHand handA = new PokerHand(hands.remove(0));
PokerHand handB;
if (hands.size() == 1) { return new PokerHand(hands.remove(0)); }
else if (hands.size() > 2) { handB = compareHands(hands); }
else { handB = new PokerHand(hands.remove(0)); }
if (handA.compareTo(handB) > 0) { return handA; }
else { return handB; }
}
private void addCards(List<String> cardStrings) {
for (String cardString : cardStrings) { deck.add(new PokerCard(cardString)); }
}
}
As MyPokerPlayer
shows, there is a default constructor: 如MyPokerPlayer
所示,有一个默认的构造函数:
MyPokerPlayer() { }
I have tried adding public
in front of it, just in case, but that doesn't help either. 我试图在其前面添加public
,以防万一,但这也无济于事。
There seems to be a mismatch between the class in the exception ( Class blake.MyPokerPlayer must have a default constructor
) and the class in your code snippet ( player.MyPokerPlayer
) 似乎是异常的类之间的不匹配( Class blake.MyPokerPlayer must have a default constructor
),并在你的代码片段类( player.MyPokerPlayer
)
Can you check the class name you're passing to loadPlayer(String)? 您可以检查传递给loadPlayer(String)的类名吗?
Additionally, the constructor MyPokerPlayer is package-scoped, which could cause the runtime to fail reflective invocation (please make the constructor public). 此外,构造函数MyPokerPlayer是包范围的,这可能导致运行时失败反射式调用(请使构造函数公开)。 This would typically be an issue as the pokergame package is not the one defining the class. 这通常是个问题,因为pokergame软件包不是定义类的软件包。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.