Results 1 to 6 of 6
  1. #1
    g2mediagroup is offline Member
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Unhappy Need help...blackjack not working.

    Hello,
    I'm VERY new to programming. If anyone could look at my code and give me some help, I'd really appreciate it. I'm using JGrasp as that was what the instructor has us using. Here's the "run" output (the GUI window doesn't show??)


    ----jGRASP exec: java Blackjack
    Exception in thread "main" java.lang.NullPointerException
    at java.awt.Container.addImpl(Container.java:1041)
    at java.awt.Container.add(Container.java:365)
    at Blackjack.<init>(Blackjack.java:85)
    at Blackjack.main(Blackjack.java:260)

    ----jGRASP wedge: exit code for process is 1.
    ----jGRASP: operation complete.




    ...and my code:





    // Class: COMT20011 - Java Programming

    // Name: George

    // Project: #4

    // Date: 2011.04.17

    //************************************************** ***********************************
    // Blackjack: This program will determine user's dealt cards (2) compared to the dealer's.
    //The game is based on the card values adding up to as close to 21 as possible
    //without going over. The card hand that has the highest value close to 21 wins.
    //If either hand goes over 21, that hand automatically loses. If both hands end
    //with the same value, then it will be a tie/draw. The user can choose to "call" or "hit".
    //Call will complete the user's hand, no additional
    //cards will be recieved. Hit will give the user another card. Ace is assumed always 11 points.
    //************************************************** ***********************************


    import java.util.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.lang.*;

    public class Blackjack extends JFrame
    {
    private static final int WIDTH = 400;
    private static final int HEIGHT = 300;

    private JLabel card1L, card2L, cardOtherL, totalL;
    private JTextField card1TF, card2TF, cardOtherTF, totalTF;
    private JButton turnCard1B, turnCard2B, hitB, callB;

    private TurnCard1ButtonHandler tc1bHandler;
    private TurnCard2ButtonHandler tc2bHandler;
    private HitButtonHandler hbHandler;
    private CallButtonHandler cbHandler;

    JTextField faceValue = new JTextField(5);
    int dealerCard1, dealerCard2, playerCard1, playerCard2, dealerOtherCard, playerOtherCard,playerTotal, dealerTotal;

    //constructor
    public Blackjack()
    {
    //Create Labels
    card1L = new JLabel("Card 1");
    card2L = new JLabel("Card 2");
    cardOtherL = new JLabel("Other Card(s)");
    totalL = new JLabel("Your Hand Total");

    //Create Hit Button
    hitB = new JButton("Hit");
    hbHandler = new HitButtonHandler();
    hitB.addActionListener(hbHandler);

    //Create Call Button
    callB = new JButton("Call");
    cbHandler = new CallButtonHandler();
    callB.addActionListener(cbHandler);

    //Create Turn Card 1 Button
    turnCard1B = new JButton("Turn Over Card 1");
    tc1bHandler = new TurnCard1ButtonHandler();
    turnCard1B.addActionListener(tc1bHandler);

    //Create Turn Card 2 Button
    turnCard2B = new JButton("Turn Over Card 2");
    tc2bHandler = new TurnCard2ButtonHandler();
    turnCard2B.addActionListener(tc2bHandler);

    //Set the title of window
    setTitle("Simple Blackjack Game");

    //Get the container
    Container pane = getContentPane();

    //Set the pane layout
    pane.setLayout(new GridLayout(4, 3));

    //Place the components in the pane
    pane.add(card1L);
    pane.add(card2L);
    pane.add(cardOtherL);
    pane.add(card1TF);
    pane.add(card2TF);
    pane.add(cardOtherTF);
    pane.add(card1TF);
    pane.add(hitB);
    pane.add(callB);
    pane.add(totalL);


    //Set the size of the window and display it
    setSize(WIDTH, HEIGHT);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    }








    //action performed for button handler
    private class HitButtonHandler implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
    playerOtherCard = GetCard();
    cardOtherTF = faceValue;
    playerTotal += playerOtherCard;
    if (playerTotal >21)
    {
    JOptionPane.showMessageDialog(null, "Your hand exceeded 21", "YOU LOST",JOptionPane.INFORMATION_MESSAGE);
    }
    totalTF.setText("" + playerTotal);

    }
    }


    //action performed for button handler
    private class CallButtonHandler implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
    if (playerTotal < dealerTotal)
    {
    JOptionPane.showMessageDialog(null, "Your hand is "
    + playerTotal
    + "\n"
    + "The dealers hand is "
    + dealerTotal
    ,"YOU LOST",JOptionPane.INFORMATION_MESSAGE);
    }
    if (playerTotal > dealerTotal && playerTotal <= 21)
    {
    JOptionPane.showMessageDialog(null, "Your hand is "
    + playerTotal
    + "\n"
    + "The dealers hand is "
    + dealerTotal
    ,"YOU WON",JOptionPane.INFORMATION_MESSAGE);
    }
    if (playerTotal == dealerTotal)
    {
    JOptionPane.showMessageDialog(null, "The game is a tie","TIE",JOptionPane.INFORMATION_MESSAGE);
    }

    }
    }


    //action performed for button handler
    private class TurnCard1ButtonHandler implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
    playerCard1 = GetCard();
    playerTotal = playerCard1;
    card1TF.setText("" + faceValue);
    totalTF.setText("" + playerTotal);
    }
    }


    //action performed for button handler
    private class TurnCard2ButtonHandler implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
    playerCard2 = GetCard();
    playerTotal += playerCard2;
    card2TF.setText("" + faceValue);
    totalTF.setText("" + playerTotal);
    }
    }







    //function of player hand random card generator
    public int GetCard()
    {

    int pointValue = 0;
    Random randCard = new Random();
    int dealtCard = randCard.nextInt(12) + 2;
    switch(dealtCard)
    {
    case 2: faceValue.setText("2");
    pointValue = 2;
    break;
    case 3: faceValue.setText("3");
    pointValue = 3;
    break;
    case 4: faceValue.setText("4");
    pointValue = 4;
    break;
    case 5: faceValue.setText("5");
    pointValue = 5;
    break;
    case 6: faceValue.setText("6");
    pointValue = 6;
    break;
    case 7: faceValue.setText("7");
    pointValue = 7;
    break;
    case 8: faceValue.setText("8");
    pointValue = 8;
    break;
    case 9: faceValue.setText("9");
    pointValue = 9;
    break;
    case 10: faceValue.setText("10");
    pointValue = 10;
    break;
    case 11: faceValue.setText("JACK");
    pointValue = 10;
    break;
    case 12: faceValue.setText("QUEEN");
    pointValue = 10;
    break;
    case 13: faceValue.setText("KING");
    pointValue = 10;
    break;
    case 14: faceValue.setText("ACE");
    pointValue = 11;
    break;
    }
    return pointValue;
    }

    //function of dealer handl
    //deals 2 cards, check value(if <=10, do "hit")
    //check value and repeat if necessary
    public void GetDealersHand()
    {
    dealerCard1 = GetCard();
    dealerCard2 = GetCard();
    dealerTotal = dealerCard1 + dealerCard2;

    if (dealerTotal <= 10)
    {
    dealerOtherCard = GetCard();
    dealerTotal += dealerOtherCard;
    }
    }



    public static void main(String[] args)
    {
    Blackjack blackjack21 = new Blackjack();
    }
    }
    Attached Files Attached Files

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    the class Blackjack try to use some fields that are never initialized, for examle card1TF. to initialize this field use for example use

    card1TF = new JTextField();

  3. #3
    g2mediagroup is offline Member
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Question

    Thanks, j2me64!

    I initialized the fields like you suggested and now the window works! I did some further tweaks to get the other buttons to show up, but now when I press the turnCard1B and turnCard2B I get this in the associated JTextField(ie card1TF, card2TF) :

    javax.swing.JTextField[,0,0,0x0,invalid,layout=javax.swing.plaf.basic.Bas icTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0 .0,border=com.apple.laf.AquaTextFieldBorder@5d3e75 4f,flags=288,maximumSize=,minimumSize=,preferredSi ze=,caretColor=javax.swing.plaf.ColorUIResource[r=0,g=0,b=0],disabledTextColor=javax.swing.plaf.ColorUIResourc e[r=128,g=128,b=128],editable=true,margin=javax.swing.plaf.InsetsUIRes ource[top=0,left=0,bottom=0,right=0],selectedTextColor=com.apple.laf.AquaImageFactory$ SystemColorProxy[r=0,g=0,b=0],selectionColor=com.apple.laf.AquaImageFactory$Sys temColorProxy[r=173,g=204,b=251],columns=5,columnWidth=0,command=,horizontalAlignm ent=LEADING]

    ...totalTF and cardOtherTF work just fine...??

    Also, the dealer's hand always equals 0...??

    Here's updated code:


    // Class: COMT20011 - Java Programming

    // Name: George

    // Project: #4

    // Date: 2011.04.17

    //************************************************** ***********************************
    // Blackjack: This program will determine user's dealt cards (2) compared to the dealer's.
    //The game is based on the card values adding up to as close to 21 as possible
    //without going over. The card hand that has the highest value close to 21 wins.
    //If either hand goes over 21, that hand automatically loses. If both hands end
    //with the same value, then it will be a tie/draw. The user can choose to "call" or "hit".
    //Call will complete the user's hand, no additional
    //cards will be recieved. Hit will give the user another card. Ace is always 11 points.
    //************************************************** ***********************************


    import java.util.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.lang.*;

    public class Blackjack extends JFrame
    {
    private static final int WIDTH = 400;
    private static final int HEIGHT = 300;

    private JLabel card1L, card2L, cardOtherL, totalL;
    private JTextField card1TF = new JTextField(), card2TF = new JTextField(), cardOtherTF = new JTextField(), totalTF = new JTextField();
    private JButton turnCard1B, turnCard2B, hitB, callB, replayB;

    private TurnCard1ButtonHandler tc1bHandler;
    private TurnCard2ButtonHandler tc2bHandler;
    private HitButtonHandler hbHandler;
    private CallButtonHandler cbHandler;
    private ReplayButtonHandler rbHandler;

    JTextField faceValue = new JTextField(5);
    int dealerCard1, dealerCard2, playerCard1, playerCard2, dealerOtherCard, playerOtherCard,playerTotal, dealerTotal;

    //constructor
    public Blackjack()
    {
    //Create Labels
    card1L = new JLabel("Card 1", JLabel.CENTER);
    card2L = new JLabel("Card 2", JLabel.CENTER);
    cardOtherL = new JLabel("Other Card(s)", JLabel.CENTER);
    totalL = new JLabel("Your Hand Total", JLabel.RIGHT);

    //Create Hit Button
    hitB = new JButton("Hit");
    hbHandler = new HitButtonHandler();
    hitB.addActionListener(hbHandler);

    //Create Call Button
    callB = new JButton("Call");
    cbHandler = new CallButtonHandler();
    callB.addActionListener(cbHandler);

    //Create Replay Button
    replayB = new JButton("Replay");
    rbHandler = new ReplayButtonHandler();
    replayB.addActionListener(rbHandler);

    //Create Turn Card 1 Button
    turnCard1B = new JButton("Turn Over Card 1");
    tc1bHandler = new TurnCard1ButtonHandler();
    turnCard1B.addActionListener(tc1bHandler);

    //Create Turn Card 2 Button
    turnCard2B = new JButton("Turn Over Card 2");
    tc2bHandler = new TurnCard2ButtonHandler();
    turnCard2B.addActionListener(tc2bHandler);

    //Set the title of window
    setTitle("Simple Blackjack Game");

    //Get the container
    Container pane = getContentPane();

    //Set the pane layout
    pane.setLayout(new GridLayout(5, 3));

    //Place the components in the pane
    pane.add(card1L);
    pane.add(card2L);
    pane.add(cardOtherL);
    pane.add(card1TF);
    pane.add(card2TF);
    pane.add(cardOtherTF);
    pane.add(turnCard1B);
    pane.add(turnCard2B);
    pane.add(hitB);
    pane.add(callB);
    pane.add(totalL);
    pane.add(totalTF);
    pane.add(replayB);


    //Set the size of the window and display it
    setSize(WIDTH, HEIGHT);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    }








    //action performed for button handler
    private class HitButtonHandler implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
    playerOtherCard = GetCard();
    cardOtherTF.setText("" + faceValue);
    playerTotal += playerOtherCard;
    totalTF.setText("" + playerTotal);

    if (playerTotal >21)
    {
    JOptionPane.showMessageDialog(null, "Your hand exceeded 21", "YOU LOST",JOptionPane.INFORMATION_MESSAGE);
    }
    cardOtherTF.setText("" + playerOtherCard);
    totalTF.setText("" + playerTotal);

    }
    }


    //action performed for button handler
    private class CallButtonHandler implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
    if (playerTotal < dealerTotal)
    {
    JOptionPane.showMessageDialog(null, "Your hand is "
    + playerTotal
    + "\n"
    + "The dealers hand is "
    + dealerTotal
    ,"YOU LOST",JOptionPane.INFORMATION_MESSAGE);
    }
    if (playerTotal > dealerTotal && playerTotal <= 21)
    {
    JOptionPane.showMessageDialog(null, "Your hand is "
    + playerTotal
    + "\n"
    + "The dealers hand is "
    + dealerTotal
    ,"YOU WON",JOptionPane.INFORMATION_MESSAGE);
    }
    if (playerTotal == dealerTotal)
    {
    JOptionPane.showMessageDialog(null, "The game is a tie","TIE",JOptionPane.INFORMATION_MESSAGE);
    }

    }
    }


    //action performed for button handler
    private class TurnCard1ButtonHandler implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
    playerCard1 = GetCard();
    playerTotal = playerCard1;
    card1TF.setText("" + faceValue);
    totalTF.setText("" + playerTotal);
    }
    }


    //action performed for button handler
    private class TurnCard2ButtonHandler implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
    playerCard2 = GetCard();
    playerTotal += playerCard2;
    card2TF.setText("" + faceValue);
    totalTF.setText("" + playerTotal);
    }
    }


    //action performed for button handler
    private class ReplayButtonHandler implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
    playerCard1 = 0;
    playerCard2 = 0;
    playerOtherCard = 0;
    dealerCard1 = 0;
    dealerCard2 = 0;
    dealerOtherCard = 0;
    playerTotal = 0;
    dealerTotal = 0;
    card1TF.setText("");
    card2TF.setText("");
    cardOtherTF.setText("");
    totalTF.setText("");
    }
    }





    //function of player hand random card generator
    public int GetCard()
    {
    int pointValue = 0;
    Random randCard = new Random();
    int dealtCard = randCard.nextInt(12) + 2;
    switch(dealtCard)
    {
    case 2: faceValue.setText("2");
    pointValue = 2;
    break;
    case 3: faceValue.setText("3");
    pointValue = 3;
    break;
    case 4: faceValue.setText("4");
    pointValue = 4;
    break;
    case 5: faceValue.setText("5");
    pointValue = 5;
    break;
    case 6: faceValue.setText("6");
    pointValue = 6;
    break;
    case 7: faceValue.setText("7");
    pointValue = 7;
    break;
    case 8: faceValue.setText("8");
    pointValue = 8;
    break;
    case 9: faceValue.setText("9");
    pointValue = 9;
    break;
    case 10: faceValue.setText("10");
    pointValue = 10;
    break;
    case 11: faceValue.setText("JACK");
    pointValue = 10;
    break;
    case 12: faceValue.setText("QUEEN");
    pointValue = 10;
    break;
    case 13: faceValue.setText("KING");
    pointValue = 10;
    break;
    case 14: faceValue.setText("ACE");
    pointValue = 11;
    break;
    }
    return pointValue;
    }

    //function of dealer handl
    //deals 2 cards, check value(if <=10, do "hit")
    //check value and repeat if necessary
    public void GetDealersHand()
    {
    dealerCard1 = GetCard();
    dealerCard2 = GetCard();
    dealerTotal = dealerCard1 + dealerCard2;

    if (dealerTotal <= 10)
    {
    dealerOtherCard = GetCard();
    dealerTotal += dealerOtherCard;
    }
    }



    public static void main(String[] args)
    {
    Blackjack blackjack21 = new Blackjack();
    }
    }
    Attached Files Attached Files
    Last edited by g2mediagroup; 05-02-2011 at 01:50 PM. Reason: attachment added

  4. #4
    g2mediagroup is offline Member
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Default

    ...I was testing and found a few other bugs:

    (see attached screen captures)

    The "Turn Over Card 1" and "Turn Over Card 2" buttons can be clicked on endlessly; this should be a once per game (reset with the "Replay" button) event...do you have any advice to accomplish this?

    And I can get the "Other Card(s)" (JTextField cardOtherTF) to have the same error displayed as card1TF and card2TF IF I get the player hand >21...???

    Thanks again!
    Attached Thumbnails Attached Thumbnails Need help...blackjack not working.-screen-shot-2011-05-02-9.23.05-am.jpg   Need help...blackjack not working.-screen-shot-2011-05-02-9.23.23-am.jpg   Need help...blackjack not working.-screen-shot-2011-05-02-9.23.53-am.jpg   Need help...blackjack not working.-screen-shot-2011-05-02-9.24.07-am.jpg  

  5. #5
    g2mediagroup is offline Member
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Default

    fixed the endless button clicks with:

    turnCard1B.setEnabled(false);

    When "Replay" is clicked:

    turnCard1B.setEnabled(true);

  6. #6
    g2mediagroup is offline Member
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

Similar Threads

  1. Creating a game of blackjack
    By adjit in forum New To Java
    Replies: 3
    Last Post: 04-29-2011, 02:14 PM
  2. Help With BlackJack Game !
    By TangoMango in forum New To Java
    Replies: 1
    Last Post: 01-24-2011, 05:40 PM
  3. blackjack help
    By jordaejava in forum New To Java
    Replies: 1
    Last Post: 12-11-2009, 05:44 AM
  4. Multiplayer BlackJack Applet
    By lostwake in forum Java Applets
    Replies: 0
    Last Post: 07-23-2009, 11:04 PM
  5. Help with simple Blackjack Program?
    By meeper3000 in forum New To Java
    Replies: 2
    Last Post: 04-26-2009, 09:46 AM

Tags for this Thread

Posting Permissions

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