我正在开发一个通过我创建的 JFrame 运行的程序,我只是在调用代码中的某些方法时遇到了一些问题。我正在运行一种基于评估输出某些内容的方法的问题,我不断在相同的位置获得空指针。
JButton btnDealCards = new JButton("Deal Cards");
btnDealCards.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
displayYourHand.setText("");
output = "";
couples = 0;
for (int i = 0; i < hand.length; i++) {
Card1 dealt = dealHand();
if (dealt != null) {
hand[i] = dealt;
displayYourHand.setText(displayYourHand.getText()
+ hand[i].toString() + "\n");
} else {
displayYourHand.setText("NOT ENOUGH CARDS TO DEAL");
status.setText("Shuffle cards to continue");
return;
}
}
// totalHand();
// pairs();
// twoPair();
// threeOfAKind();
}
});
btnDealCards.setBounds(336, 192, 98, 26);
contentPane.add(btnDealCards);
JButton btnShuffleCards = new JButton("Shuffle Cards");
btnShuffleCards.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
shuffle();
displayYourHand.setText("The Deck Has Been Shuffled");
}
});
btnShuffleCards.setBounds(314, 229, 147, 23);
contentPane.add(btnShuffleCards);
}
public void shuffle() {
for (int first = 0; first < deck.length; first++) {
int second = randomNumbers.nextInt(52);
Card1 temp = deck[first];
deck[first] = deck[second];
deck[second] = temp;
}
btnDealCards.setEnabled(true);
}
public Card1 dealHand() {
if (currentCard < deck.length)
return deck[currentCard++];
else {
btnDealCards.setEnabled(false);
return null;
}
}
public void pairs() {
for (int k = 0; k < faces.length; k++)
if (numbers[k] == 2) {
output += "" + ("Pair of " + faces[k] + "'s ");
couples++;
}
status.setText(output);
}
第一个是我的 actionlistener 和 actionperformer,接下来的三个是我希望执行的动作,它们会吐出空指针。问题始终是 for 循环之后的行,例如“status.setText(output);” 或“btnDealCards.setEnabled(true);”。我想我需要将这些语句变成返回语句,但这是我唯一的想法。提前致谢!