gpt4 book ai didi

java - JButton 监听器调用类中的方法给出 NullPointerException

转载 作者:行者123 更新时间:2023-12-01 12:08:13 24 4
gpt4 key购买 nike

这里是java新手,我正在尝试使用GUI进行我的第一个项目。我有一个包含 gamePlay() 和 userTurn() 的 GameConsole 类。我有一个 ButtonListener 类,它使用调用 userTurn() 的 actionListener 构造按钮。但是,每次按下按钮时,我都会收到 NullPointerException。为什么会发生这种情况以及我该如何解决它?

相关代码:

public static final void main (String[] args){
GameConsole game = new GameConsole();
game.gamePlay();
}

public class GameConsole {
public Player user;
public Player dealer;
public Deck playingDeck;
ValidateInput validate = new ValidateInput();

public void gamePlay(){
//get info from user
user.addToHand(playingDeck.getTopCard());
dealer.addToHand(playingDeck.getTopCard());
user.addToHand(playingDeck.getTopCard());
dealer.addToHand(playingDeck.getTopCard());

userTurn();
}

public void userTurn(){
boolean turn = true;
do{ //the code breaks at this point. On the first round of gameplay I get the exception pasted
//below. The second round (after the button is pressed) I get an
//Exception in thread "AWT-EventQueue-0 bringing me back here
JOptionPane.showMessageDialog(null, "The cards you were dealt are: \n" + user.printHand());

if(user.sumOfHand() == 21){ //no need to continue if user has 21
System.out.println("You win this round.");
break;
} else if (user.sumOfHand() > 21){
System.out.println("You have busted. You lose this round.");
break;
}


String HSInput = null;
for(int x = 0; x < 1;){
HSInput = JOptionPane.showInputDialog("\nThe dealer is showing a " + dealer.getTopCard()
+ "\nYou are currently at " + user.sumOfHand()
+ "\n\nWhat would you like to do?\nHit (H) or Stay (S)?");
if(validate.containDesiredString(HSInput, "HhSs")) //only accept h and s either caps
x++;
}


if(HSInput.equalsIgnoreCase("H")){ //if the user wants to stay
user.addToHand(playingDeck.getTopCard()); //deal new card then repeat loop
}
else {
turn = false;
}
} while (turn == true);
}

还有 ButtonListener 类...

public class ButtonListener implements ActionListener {

JButton newRoundButton;

public ButtonListener(JPanel endGamePanel){

newRoundButton = new JButton("Start another round");
newRoundButton.setPreferredSize(new Dimension(150, 50));
endGamePanel.add(newRoundButton);
newRoundButton.addActionListener(this);

}


@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == newRoundButton){
System.out.println("Hi there for a new round");
//I know that this method is entered into because this line will print
GameConsole game = new GameConsole();
game.userTurn();
}
}

我非常迷失方向,在过去的两天里一直在原地踏步。现在程序甚至不会进入 userTurn()。我不知道我做了什么,因为几个小时前这还不是问题。不管怎样,我的最终问题是我无法让 ButtonListener 调用 userTurn() 类。为什么我现在会收到 userTurn() 的 NullPointerException 异常?我该怎么做才能让 ActionListener 调用 userTurn() 而不给出 NullPointerException

-绝望地迷失......感谢任何帮助!

编辑:堆栈跟踪线程“main”jav 中出现异常

a.lang.NullPointerException
at blackjackControls.GameConsole.userTurn(GameConsole.java:180)
at blackjackControls.GameConsole.gamePlay(GameConsole.java:119)
at blackjackControls.Main.main(Main.java:7)

因为它不再进入 userTurn() 我无法发布跟踪。它确实声明它是 AWT 中的一个线程

ButtonListener 类已与现有 ResultPane 类合并。完整代码为:

public class ResultPanel extends JFrame {

JFrame frame = new JFrame("Blackjack Game");
JPanel endGamePanel = new JPanel();
ImageIcon image;
JLabel lbl;
JButton newRoundButton;
JButton endButton;

public ResultPanel(){

frame.setSize(800, 600);
frame.getContentPane().add(endGamePanel); //add the panel to the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JButton newRoundButton= new JButton("Start another round");
newRoundButton.setPreferredSize(new Dimension(150, 50));
// newRoundButton.addActionListener();
newRoundButton.addActionListener(new ActionListener(){

@Override
public void actionPerformed(ActionEvent e) {

System.out.println("Hi there for a new round");
GameConsole game = new GameConsole();
game.userTurn();
}
});


frame.setVisible(true);
}

public void winGame(){
image = new ImageIcon(getClass().getResource("win.jpg"));
lbl = new JLabel (image);
endGamePanel.add(lbl);

frame.setVisible(true);
}

最佳答案

据我所知,您在主程序中创建了一个新的 GameConsole 对象,因此我假设这是整个游戏运行的对象。如果是这种情况,为什么您的按钮会创建一个新的 GameConsole 对象?我假设您收到 NullPointerException ,因为您的按钮在没有 User 的新 GameConsole 对象上调用 userTurn() 方法尚未对象,因此 user.sumOfHand() 例如会抛出异常。

您的按钮应该调用您在 main 中创建的 GameConsole 对象上的 userTurn 方法,而不是在新对象上调用。

代码建议:

JButton newRoundButton= new JButton("Start another round");
newRoundButton.setPreferedSize(new Dimension(150, 50));
newRoundButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Hi there for a new round");
game.userTurn();
}
});
endGamePanel.add(newRoundButton);

这是假设构建您的面板的任何类都知道您的 GameConsole 对象。如果此类是 GameConsole 对象本身,只需删除 game。 如果在您的 GameConsole 对象中创建了该类的对象,只需传递您的GameConsole 对象指向该类,并在该类构造函数中使用参数 GameConsole game ,如果需要,请将其作为该类中的字段。要从该对象传递您的 GameConsole 对象,请传递 this

关于java - JButton 监听器调用类中的方法给出 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27434229/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com