1 Attachment(s)
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();
}
}