0

I'm new to that site. Recently I started to work on a game on java (Blackjack), and I came across some difficulties. This time I need your help.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Game.hitPlayer(Game.java:263)
at Game.deal(Game.java:235)
at Game.actionPerformed(Game.java:169)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

And this is the code itself:

public class Game extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;

private JFrame frmBlackjack;
private JButton btnDeal = new JButton("DEAL HAND");
private JButton btnHit = new JButton("HIT");
private JButton btnStand = new JButton("STAND");
private final JLabel lblBet = new JLabel("Place your bet:");
private JTextField textField;

public JPanel dealerPanel = new JPanel();
public JPanel playerPanel = new JPanel();
public JLabel lblStatus = new JLabel("");
public JLabel lblDealer = new JLabel("Dealer:");
public JLabel lblPlayer = new JLabel("Player:");
public JLabel lblMoney = new JLabel("Money: 100$");

private Hand playerHand;
private Hand dealerHand;
private Deck deck;
private Card newCard;
private Double money = 100.0;
private Double bet = 0.0;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Game window = new Game();
                window.frmBlackjack.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public Game() 
{
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() 
{
    frmBlackjack = new JFrame();
    frmBlackjack.getContentPane().setFont(new Font("Tahoma", Font.PLAIN, 11));
    frmBlackjack.setTitle("Blackjack (21)");
    frmBlackjack.setBounds(100, 100, 600, 520);
    frmBlackjack.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frmBlackjack.getContentPane().setLayout(null);

    btnDeal.setFont(new Font("Tahoma", Font.BOLD, 11));
    btnDeal.setBounds(10, 410, 150, 60);
    btnDeal.addActionListener(this);
    frmBlackjack.getContentPane().add(btnDeal);
    btnDeal.setEnabled(false);

    btnHit.setFont(new Font("Tahoma", Font.BOLD, 11));
    btnHit.setBounds(264, 410, 150, 60);
    btnHit.addActionListener(this);
    frmBlackjack.getContentPane().add(btnHit);
    btnHit.setEnabled(false);

    btnStand.setFont(new Font("Tahoma", Font.BOLD, 11));
    btnStand.setBounds(424, 410, 150, 60);
    btnStand.addActionListener(this);
    frmBlackjack.getContentPane().add(btnStand);
    btnStand.setEnabled(false);

    dealerPanel.setBounds(113, 11, 440, 160);
    frmBlackjack.getContentPane().add(dealerPanel);
    dealerPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

    playerPanel.setBounds(113, 185, 440, 160);
    frmBlackjack.getContentPane().add(playerPanel);
    playerPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

    lblStatus.setFont(new Font("Agency FB", Font.PLAIN, 30));
    lblStatus.setHorizontalAlignment(SwingConstants.CENTER);
    lblStatus.setBounds(339, 351, 220, 50);
    frmBlackjack.getContentPane().add(lblStatus);

    lblDealer.setHorizontalAlignment(SwingConstants.CENTER);
    lblDealer.setFont(new Font("Agency FB", Font.PLAIN, 20));
    lblDealer.setBounds(10, 64, 93, 50);
    frmBlackjack.getContentPane().add(lblDealer);

    lblPlayer.setFont(new Font("Agency FB", Font.PLAIN, 20));
    lblPlayer.setHorizontalAlignment(SwingConstants.CENTER);
    lblPlayer.setBounds(10, 245, 93, 50);
    frmBlackjack.getContentPane().add(lblPlayer);

    lblMoney.setFont(new Font("Agency FB", Font.PLAIN, 20));
    lblMoney.setHorizontalAlignment(SwingConstants.LEFT);
    lblMoney.setBounds(40, 349, 75, 50);
    frmBlackjack.getContentPane().add(lblMoney);

    lblBet.setHorizontalAlignment(SwingConstants.CENTER);
    lblBet.setFont(new Font("Agency FB", Font.PLAIN, 20));
    lblBet.setBounds(135, 351, 100, 50);
    frmBlackjack.getContentPane().add(lblBet);

    textField = new JTextField();
    textField.setBounds(245, 368, 45, 20);
    frmBlackjack.getContentPane().add(textField);
    textField.setColumns(10);
    textField.addActionListener(this);
    textField.setEnabled(true);
}


@Override
public void actionPerformed(ActionEvent e)
{
    if (e.getSource() == btnHit) 
    {
        hitPlayer();
        if (playerHand.getBlackjackValue() > 21) 
        {
            checkWinner();
            btnHit.setEnabled(false);
            btnStand.setEnabled(false);
            btnDeal.setEnabled(false);
        }
    }

    if (e.getSource() == btnStand) 
    {
        while (dealerHand.getBlackjackValue() < 17 || playerHand.getBlackjackValue() > dealerHand.getBlackjackValue()) 
        {
            hitDealer();
        }
        checkWinner();
        btnHit.setEnabled(false);
        btnStand.setEnabled(false);
        btnDeal.setEnabled(false);
    }

    if (e.getSource() == btnDeal) 
    {
        deal();
        lblStatus.setText("");
        btnHit.setEnabled(true);
        btnStand.setEnabled(true);
        btnDeal.setEnabled(false);
    }   

    if (e.getSource() == textField)
    {
        String curBet = textField.getText();
        bet = Double.parseDouble(curBet); 
        textField.setEnabled(false);
        btnDeal.setEnabled(true);
    }
}

private void checkWinner() 
{
    dealerPanel.removeAll();
    for (int i = 0; i < dealerHand.getCardCount(); i++) 
    {
        dealerPanel.add(new JLabel(new ImageIcon("C:/Users/Talker/Desktop/cards/" + dealerHand.getCard(i).toString() + ".png")));
    }
    textField.setEnabled(true);
    if (playerHand.getBlackjackValue() > 21) 
    {
        lblStatus.setText("You Lost! (Over 21)");
        money -= bet;
        lblMoney.setText("Money:" + money + "$.");
    } 
    else if (dealerHand.getBlackjackValue() > 21) 
    {
        lblStatus.setText("Dealer Lost! (Over 21)");
        money += bet;
        lblMoney.setText("Money:" + money + "$.");
    } 
    else if (dealerHand.getBlackjackValue() < playerHand.getBlackjackValue()) 
    {
        lblStatus.setText("You Won!");
        money += bet;
        lblMoney.setText("Money:" + money + "$.");
    } 
    else if (dealerHand.getBlackjackValue() > playerHand.getBlackjackValue())
    {
        lblStatus.setText("Dealer Won!");
        money -= bet;
        lblMoney.setText("Money:" + money + "$.");
    }
    else 
        lblStatus.setText("It's a tie!");
}

private void deal() 
{
    playerPanel.removeAll();
    dealerPanel.removeAll();
    playerPanel.updateUI();
    dealerPanel.updateUI();
    //playerHand.clear();
    //dealerHand.clear();

    if (deck == null || deck.cardsLeft() < 15) 
    {
        deck = new Deck();
        deck.shuffleDeck();
    }
    hitPlayer();
    hitDealerDown();
    dealerHand.addCard(getNewCard());
    hitPlayer();
    hitDealer();

    //Check for blackjack!
    if(dealerHand.getBlackjackValue() == 21 && dealerHand.getBlackjackValue() != playerHand.getBlackjackValue())
    {
        lblStatus.setText("Blackjack! You Lost!");
        money -= bet;
        lblMoney.setText("Money:" + money + "$.");
    }
    if(playerHand.getBlackjackValue() == 21 && playerHand.getBlackjackValue() != dealerHand.getBlackjackValue())
    {
        lblStatus.setText("Blackjack! You Won!");
        money = bet + bet/2;
        lblMoney.setText("Money:" + money + "$.");
    }
    if(dealerHand.getBlackjackValue() == 21 && playerHand.getBlackjackValue() == 21)
        lblStatus.setText("It's a tie!");
}

private void hitPlayer() 
{
    Card newCard = deck.dealCard();
    playerHand.addCard(newCard);
    playerPanel.add(new JLabel(new ImageIcon("C:/Users/Talker/Desktop/cards/" + newCard.toString() + ".png")));
    playerPanel.updateUI();
}

private void hitDealerDown() 
{
    setNewCard(deck.dealCard());
    dealerPanel.add(new JLabel(new ImageIcon("C:/Users/Talker/Desktop/cards/b2fv.png")));
    dealerPanel.updateUI();
}

private void hitDealer() 
{
    Card newCard = deck.dealCard();
    dealerHand.addCard(newCard);
    dealerPanel.add(new JLabel(new ImageIcon("C:/Users/Talker/Desktop/cards/" + newCard.toString() + ".png")));
    dealerPanel.updateUI();
}

public Card getNewCard() {
    return newCard;
}

public void setNewCard(Card newCard) {
    this.newCard = newCard;
}

}

What is the problem? I don't understand!

Almog Talker
  • 101
  • 9
  • Where do you initialize `playerHand`? – Tom Mar 15 '15 at 19:47
  • Your exception is a NullPointerException or NPE, and you need to learn the general concepts of how to debug this. **You should critically read your exception's stacktrace to find the line of code at fault, the line that throws the exception, and then inspect that line carefully**, find out which variable is null, and then trace back into your code to see why. You will run into these again and again, trust me. – Hovercraft Full Of Eels Mar 15 '15 at 19:54
  • For example, the stacktrace gives you several lines in your program to look at, including `Game.hitPlayer(Game.java:263)` and `Game.deal(Game.java:235)`. – Hovercraft Full Of Eels Mar 15 '15 at 19:55

1 Answers1

0

You need to initialize your instance variables (playerHand, dealerHand, deck, etc.) in the Game constructor (or in your initialize() method, which you are calling from the constructor). You are getting that NullPointerException because the default (uninitialized) value of a (non-primitive) instance variable is null.

wickstopher
  • 981
  • 6
  • 18