Results 1 to 4 of 4
  1. #1
    SilverSoul is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Need help with BlackJack Program

    Im doing a BlackJack Program and I keep getting the java.lang.NullPointerException. Can somebody please help me, I'm so lost

    Heres the Main Program:


    import java.util.Collections;
    import java.util.Vector;


    public class BJGame {

    private Vector<Card> deck;
    private Vector<Card> playerCards;
    private Vector<Card> dealerCards;

    private int playerPoint;
    private int dealerPoint;

    private boolean gameEnd;



    /*
    * result equals to
    * 0 - tie game
    * 1 - player wins
    * -1 - player loses
    */
    private int result;

    public BJGame() {

    // Initialize properties
    Vector<Card> deck= new Vector<Card>(52);
    Vector<Card> playerCards=new Vector<Card>();
    Vector<Card> dealerCards=new Vector<Card>();
    playerPoint=0;
    dealerPoint=0;
    gameEnd=true;
    result=0;

    // Generate a deck of cards
    generateDeck();

    // shuffle a deck
    Collections.shuffle(deck);
    }

    /*
    * game starts
    *
    */
    public void start() {

    // Draw 4 cards from a deck
    // add 2 cards to playerCards
    // add 2 cards to dealerCards

    // update playerPoints and dealerPoints
    // update gameEnd and result if player or dealer // get BJ (21)

    getPlayerCards();
    getDealerCards();


    playerPoint=getPlayerPoint();
    dealerPoint=getDealerPoint();

    if (playerPoint==21){
    gameEnd=true;
    result=1;
    } else if (dealerPoint==21){
    gameEnd=true;
    result=-1;
    } else if ((playerPoint==21)&&(dealerPoint==21)){
    gameEnd=true;
    result=0;
    }




    }

    /*
    * player asks for a new card
    */
    public Card hit() {



    // Draw a cards from a deck

    Card c = drawCard();

    // add it to playerCards

    // update playerPoints
    // update gameEnd and result if player point > 21

    // return the new card

    playerCards.add(c);
    playerPoint=calculatePoint(playerCards);
    if (playerPoint>21){
    gameEnd=true;
    result=-1;
    }


    return c;
    }

    /*
    * player turn ends
    */
    public void stand() {

    // while dealerís point < 17
    // draw a card from a deck
    // add it to dealerCards

    // update dealerPoints
    // update gameEnd and result

    while (dealerPoint<17){
    drawCard();
    Card c=drawCard();
    dealerCards.add(c);
    dealerPoint=(calculatePoint(dealerCards));
    if (dealerPoint>21){
    gameEnd=true;
    result=1;
    }

    }




    }

    /*
    * Return cards in player's hand
    */
    public Vector<Card> getPlayerCards() {
    Card c=drawCard();
    Card d=drawCard();
    playerCards.add(c);
    playerCards.add(d);

    return playerCards;
    }

    /*
    * Return cards in dealer's hand
    */
    public Vector<Card> getDealerCards() {
    dealerCards.add(drawCard());
    dealerCards.add(drawCard());


    return dealerCards;
    }

    /*
    * Return point of player
    */
    public int getPlayerPoint() {
    return playerPoint;
    }

    /*
    * Return point of dealer
    */
    public int getDealerPoint() {
    return dealerPoint;
    }

    /*
    * Return true if the game ends, false otherwise
    */
    public boolean isGameEnd() {
    return gameEnd;
    }

    /*
    * Return the result of the game
    * 0 - tie game
    * 1 - player wins
    * -1 - player loses
    */
    public int getResult() {
    return result;
    }

    /*
    * draw a card from the deck
    * return a new card
    */
    private Card drawCard() {
    Card c = deck.get(0);
    deck.remove(0);

    return c;
    }

    /*
    * calculate the total point of cards
    */
    private int calculatePoint(Vector<Card> cards) {

    // loop through each card to get a summation
    int total = 0;
    for(Card c: cards) {
    total += c.getPoint();
    }

    // check if cards contain Aces
    // if cards contain Aces and total point <= 11
    // add 10 to the total point



    if(playerCards.equals("A")){
    if (playerPoint<=11){
    playerPoint += 10;
    }
    }
    if(dealerCards.equals("A")){
    if(dealerPoint<=11){
    dealerPoint += 10;
    }
    }




    return total;
    }

    /*
    * add 52 cards to deck variable
    */
    private void generateDeck() {

    char[] suits = {'C', 'D', 'H', 'S'};
    String[] ranks = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};

    deck=new Vector<Card>(52);
    for(String r: ranks) {
    for(char s: suits) {
    deck.add(new Card(r,s));
    }
    }
    }

    }

    Heres the Card program to setup the Cards


    public class Card {

    private String rank;
    private char suit;

    /*
    * r - a rank of card: 2, 3, ..., 9, 10, J , Q, K, A
    * s - a suit of card:
    * 'C' for clubs
    * 'D' for diamonds
    * 'H' for hearts
    * 'S' for spades
    */
    public Card(String r, char s) {
    rank = r;
    suit = s;
    }

    /*
    * Return rank of a card
    */
    public String getRank() {
    return rank;
    }

    /*
    * Return suit of a card
    */
    public char getSuit() {
    return suit;
    }

    public String toString() {
    String str = rank+" of ";

    if(suit == 'C') {
    str += "Clubs";
    } else if(suit == 'D') {
    str += "Diamonds";
    } else if(suit == 'H') {
    str += "Hearts";
    } else if(suit == 'S') {
    str += "Spades";
    }

    return str;
    }

    /*
    * Return point for a card
    * Aces will return 1
    */
    public int getPoint() {

    int pt;
    if(rank.equals("J") || rank.equals("Q") || rank.equals("K")) {
    pt = 10;
    } else if (rank.equals("A")) {
    pt = 1;
    } else {
    pt = Integer.parseInt(rank);
    }

    return pt;
    }
    }

    and this is the program to run the Main program

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.EventQueue;
    import java.awt.FlowLayout;
    import java.awt.Font;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Vector;

    import javax.swing.BoxLayout;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.SwingConstants;
    import javax.swing.border.EmptyBorder;
    import javax.swing.border.LineBorder;

    public class BjGui {

    private BJGame bjGame;
    private JFrame frmCscBj;
    private JPanel playerPanel;
    private JPanel dealerPanel;

    private JButton hitBtn;
    private JButton standBtn;

    private JLabel dealerPts;
    private JLabel playerPts;

    private JLabel winloseStatus;
    private JButton btnPlayAgain;

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

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

    /**
    * Initialize the contents of the frame.
    */
    private void initialize() {
    frmCscBj = new JFrame();
    frmCscBj.setTitle("CSC 2310 BJ");
    frmCscBj.setResizable(false);
    frmCscBj.setBounds(100, 100, 450, 300);
    frmCscBj.setDefaultCloseOperation(JFrame.EXIT_ON_C LOSE);
    frmCscBj.getContentPane().setLayout(new BorderLayout(0, 0));

    JPanel cards = new JPanel();
    frmCscBj.getContentPane().add(cards, BorderLayout.CENTER);
    cards.setLayout(new BoxLayout(cards, BoxLayout.Y_AXIS));

    dealerPanel = new JPanel();
    cards.add(dealerPanel);
    dealerPanel.setBorder(new EmptyBorder(10, 5, 10, 5));
    dealerPanel.setBackground(Color.LIGHT_GRAY);
    dealerPanel.setLayout(new GridLayout(0, 6, 5, 5));

    JPanel dealer = new JPanel();
    dealer.setBackground(Color.LIGHT_GRAY);
    dealerPanel.add(dealer);
    dealer.setLayout(new BorderLayout(0, 0));

    JLabel lblDealer = new JLabel("Dealer Pts");
    lblDealer.setHorizontalAlignment(SwingConstants.CE NTER);
    dealer.add(lblDealer, BorderLayout.NORTH);
    lblDealer.setForeground(Color.BLACK);
    lblDealer.setFont(new Font("Tahoma", Font.PLAIN, 12));

    dealerPts = new JLabel("?");
    dealerPts.setHorizontalAlignment(SwingConstants.CE NTER);
    dealerPts.setForeground(Color.BLACK);
    dealer.add(dealerPts, BorderLayout.CENTER);

    playerPanel = new JPanel();
    cards.add(playerPanel);
    playerPanel.setBorder(new EmptyBorder(10, 5, 10, 5));
    playerPanel.setBackground(Color.DARK_GRAY);
    playerPanel.setLayout(new GridLayout(0, 6, 5, 5));

    JPanel player = new JPanel();
    player.setBackground(Color.DARK_GRAY);
    playerPanel.add(player);
    player.setLayout(new BorderLayout(0, 0));

    JLabel lblPlayer = new JLabel("Your Pts");
    lblPlayer.setHorizontalAlignment(SwingConstants.CE NTER);
    lblPlayer.setForeground(Color.WHITE);
    lblPlayer.setFont(new Font("Tahoma", Font.PLAIN, 12));
    player.add(lblPlayer, BorderLayout.NORTH);

    playerPts = new JLabel("0");
    playerPts.setHorizontalAlignment(SwingConstants.CE NTER);
    playerPts.setForeground(Color.WHITE);
    player.add(playerPts, BorderLayout.CENTER);

    JPanel center_panel = new JPanel();
    center_panel.setAlignmentX(Component.RIGHT_ALIGNME NT);
    frmCscBj.getContentPane().add(center_panel, BorderLayout.SOUTH);
    center_panel.setLayout(new BoxLayout(center_panel, BoxLayout.Y_AXIS));

    JPanel game_playing_panel = new JPanel();
    center_panel.add(game_playing_panel);
    game_playing_panel.setLayout(new FlowLayout(FlowLayout.LEFT, 15, 5));

    hitBtn = new JButton("Hit");
    hitBtn.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
    Card newCard = bjGame.hit();
    addPlayerCardUI(newCard);

    // update points
    playerPts.setText(bjGame.getPlayerPoint() + "");

    if (bjGame.isGameEnd()) {
    showGameResult();
    } else if (bjGame.getPlayerPoint() == 21) {
    playerStand();
    }
    }
    });
    game_playing_panel.add(hitBtn);

    standBtn = new JButton("Stand");
    standBtn.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    playerStand();
    }
    });
    game_playing_panel.add(standBtn);

    winloseStatus = new JLabel("");
    winloseStatus.setFont(new Font("Tahoma", Font.BOLD, 15));
    game_playing_panel.add(winloseStatus);
    winloseStatus.setVisible(false);

    btnPlayAgain = new JButton("Play again ?");
    btnPlayAgain.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    resetUI();
    gameStart();
    }
    });
    btnPlayAgain.setVisible(false);
    game_playing_panel.add(btnPlayAgain);

    gameStart();
    }

    private void gameStart() {

    bjGame = new BJGame();

    // initialize card
    bjGame.start();

    // display player card
    for (Card c : bjGame.getPlayerCards()) {
    addPlayerCardUI(c);
    }
    playerPts.setText(bjGame.getPlayerPoint() + "");


    // display dealer card
    addDealerCardUI(null);
    Card secondCard = bjGame.getDealerCards().get(1);
    addDealerCardUI(secondCard);

    if (bjGame.isGameEnd()) {
    showGameResult();
    }
    }

    private void playerStand() {
    bjGame.stand();
    showGameResult();
    }

    /*
    * add card UI to dealer panel or player panel
    */
    private void addPlayerCardUI(Card cardObj) {
    JPanel card = new JPanel();
    card.setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
    card.setBackground(Color.WHITE);
    card.setLayout(new BorderLayout(0, 0));

    JLabel suitLabel = new JLabel(cardObj.getSuit() + " ");
    suitLabel.setHorizontalAlignment(SwingConstants.RI GHT);
    card.add(suitLabel, BorderLayout.NORTH);

    JLabel rankLabel = new JLabel(cardObj.getRank());
    rankLabel.setHorizontalAlignment(SwingConstants.CE NTER);
    card.add(rankLabel, BorderLayout.CENTER);

    playerPanel.add(card);
    playerPanel.updateUI();
    }

    private void addDealerCardUI(Card cardObj) {
    JPanel card = new JPanel();
    card.setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
    card.setBackground(new Color(95, 158, 160));
    card.setLayout(new BorderLayout(0, 0));

    String suit;
    String rank;

    JLabel suitLabel = new JLabel();
    JLabel rankLabel = new JLabel();

    if (cardObj == null) {
    rank = "?";
    suit = " ";
    } else {
    suit = cardObj.getSuit() + " ";
    rank = cardObj.getRank();
    }

    suitLabel.setText(suit);
    suitLabel.setHorizontalAlignment(SwingConstants.RI GHT);
    card.add(suitLabel, BorderLayout.NORTH);

    rankLabel.setText(rank);
    rankLabel.setHorizontalAlignment(SwingConstants.CE NTER);
    card.add(rankLabel, BorderLayout.CENTER);

    dealerPanel.add(card);
    dealerPanel.updateUI();
    }

    private void showGameResult() {
    // update cards & points
    showAllDealerCards();
    dealerPts.setText(bjGame.getDealerPoint() + "");

    // update points
    dealerPts.setText(bjGame.getDealerPoint() + "");

    hitBtn.setVisible(false);
    standBtn.setVisible(false);

    String msg = "Tie game !";
    if (bjGame.getResult() > 0) {
    msg = "You win !";
    } else if (bjGame.getResult() < 0) {
    msg = "You lose !";
    }
    winloseStatus.setText(msg);

    winloseStatus.setVisible(true);
    btnPlayAgain.setVisible(true);
    }

    /*
    * play again, reset all UIs
    */
    private void resetUI() {
    // set default score
    playerPts.setText("0");
    dealerPts.setText("?");

    // remove player cards
    while (playerPanel.getComponentCount() > 1) {
    playerPanel.remove(1);
    }
    playerPanel.updateUI();

    // remove dealer cards
    clearDealerCardUI();

    winloseStatus.setText("");
    winloseStatus.setVisible(false);
    btnPlayAgain.setVisible(false);

    hitBtn.setVisible(true);
    standBtn.setVisible(true);
    }

    // remove dealer cards
    private void clearDealerCardUI() {
    while (dealerPanel.getComponentCount() > 1) {
    dealerPanel.remove(1);
    }
    dealerPanel.updateUI();
    }

    // update dealer cards
    private void showAllDealerCards() {
    clearDealerCardUI();

    Vector<Card> cards = bjGame.getDealerCards();
    for(Card c: cards) {
    addDealerCardUI(c);
    }
    }
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: Need help with BlackJack Program

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Need help with BlackJack Program

    I keep getting the java.lang.NullPointerException.
    Please post the full text of the error message.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,863
    Rep Power
    19

    Default Re: Need help with BlackJack Program

    And use the link given by Darryl to see how to repost your code in [code] tags [/code].
    That lot is impossible to read.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Simple BlackJack Program
    By Cookmeup272 in forum New To Java
    Replies: 2
    Last Post: 12-09-2011, 02:09 AM
  2. Blackjack Program Not Calculating Totals Correctly
    By skaterboy987 in forum New To Java
    Replies: 1
    Last Post: 10-19-2011, 08:59 PM
  3. Need quick help with a Java blackjack program!
    By Enigma in forum New To Java
    Replies: 11
    Last Post: 06-01-2011, 09:59 AM
  4. blackjack help
    By jordaejava in forum New To Java
    Replies: 1
    Last Post: 12-11-2009, 05:44 AM
  5. Help with simple Blackjack Program?
    By meeper3000 in forum New To Java
    Replies: 2
    Last Post: 04-26-2009, 09:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •