New to java here, and I am attempting my first project with GUI. I have a GameConsole class that contains gamePlay() and userTurn(). I have a ButtonListener class that constructs the button with an actionListener that calls userTurn(). However, every time that the button is pressed, I get a NullPointerException
. Why is this happening and what do I do to fix it?
Relevant code:
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);
}
And the ButtonListener class...
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();
}
}
I am desperately lost and have been going in circles for the last 2 days. Right now the program won't even enter the userTurn(). I have no idea what I did because a couple hours ago that wasn't a problem. Either way, my ultimate problem is that I can't get the ButtonListener to call the userTurn() class. Why am I now getting a NullPointerException
for the userTurn() and what can I do to make the ActionListener call the userTurn() without giving a NullPointerException
?
-Hopelessly Lost... ANY help is appreciated!
EDIT: Stack Trace Exception in thread "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)
since it is no longer going into the userTurn() I can't post the trace for that. It did state that it was a thread in the AWT
ButtonListener class has been combined with existing ResultPane class. Full code is:
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);
}