Am having bit of writiers block about how to fix this error. Its a assignment for college so am really just looking for tip or pointer in right direction.
Its a simple card game similar to poker.
Card.java
package pokerpkg;
public class Card
{
//Codes for the suits
public final static int SPADES = 0, HEARTS = 1, CLUBS = 2, DIAMONDS = 3;
//Codes for the non-numeric cards. Cards from 2 - 10 will have there numeric values as there code
public final static int ACE = 0, JACK = 10, QUEEN = 11, KING = 12;
//The suit of this card. Will be one the constants SPADE, HEARTS, CLUBS, or DIAMONDS
private final int suit;
//The value of this card, from 1 - 13.
private final int value;
public Card(int theValue, int theSuit)
{
//This will construct a card with a value between 1 and 13 and a suit between 0 and 3
//If the card is outside these parameters it is invalid
value = theValue;
suit = theSuit;
}//End Card
public int getSuit()
{
//Returns the code of the suit
return suit;
}//End getSuit
public int getValue()
{
//Return the code of the value
return value;
}//End getValue
public String suitToString()
{
//Returns a string of the cards suit
switch(suit)
{
case SPADES: return "Spades";
case HEARTS: return "Hearts";
case CLUBS: return "Clubs";
case DIAMONDS: return "Diamonds";
default: return "Invalid suit";
}//End Switch
}//End suitToString
public String valueToString()
{
//Returns a string of the cards value
switch(value)
{
case 0: return "Ace";
case 1: return "2";
case 2: return "3";
case 3: return "4";
case 4: return "5";
case 5: return "6";
case 6: return "7";
case 7: return "8";
case 8: return "9";
case 9: return "10";
case 10: return "Jack";
case 11: return "Queen";
case 12: return "King";
default : return "Invalid value";
}//End Switch
}//End valueToString
//Produces string of the cards value and suit
public String cardToString()
{
return valueToString() + " of " + suitToString();
}
//Method to pick random card using math.random
public Card pick()
{
double rand_suit = Math.random()*3;
double rand_value = Math.random()*12;
//Casting the random values into integers to suit the Card Constructor
Card c = new Card((int)rand_value, (int)rand_suit);
Card[] hand = new Card[4];
if(c == hand[0] || c == hand[1] || c == hand[2] || c == hand[3])
{
c.pick();
return c;
}
else
{
return c ;
}
}
}
CardSet.java
(This is pretty much hand class but have call it cardset for the assignment)
package pokerpkg;
public class CardSet
{
Card[] cardSet;
public CardSet()
{
cardSet = new Card[4];
}
//Set a card at a position in the hand
public void setCard(int position, Card c)
{
cardSet[position] = c;
}
//Function to pick four cards using the pick function from the card class
public void deal()
{
for(int i = 0; i<4; i++)
{
cardSet[i].pick();
}
}
//Change the card at a position to null and removed from the hand
public void removeCard(int position)
{
cardSet[position] = null;
}
//Replaces a card using the pick function from the card class
public void replaceCard(int position)
{
cardSet[position].pick();
}
//Returns card from certain position
public Card getCard(int position)
{
return cardSet[position];
}
//Function to display the hand as a string
public void display()
{
for(int i = 0; i<4; i++)
{
System.out.println(cardSet[i].cardToString());
}
}
}
CardGame.java
(This class is the GUI, the doDeal function is the problem i'm having i amen't sure how to start the game when the deal button is pressed)
package pokerpkg;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class CardGame extends JFrame implements ActionListener
{
JPanel cpane, scorePanel, buttonPanel;
CardPanel cardPanel;
JLabel handScore, totalScore;
JButton deal, secondDeal;
CardSet hand;
public CardGame()
{
setTitle("Simple Poker Game");
setSize(800, 800);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setContentPane(CreateContent());
setVisible(true);
}
public Container CreateContent()
{
cpane = new JPanel(new BorderLayout());
cpane.setBackground(Color.green);
scorePanel = new JPanel();
scorePanel.setBackground(Color.green);
cardPanel = new CardPanel();
cardPanel.setLayout(new GridLayout(1,4));
cardPanel.setBackground(Color.green);
buttonPanel = new JPanel();
buttonPanel.setBackground(Color.green);
handScore = new JLabel("Hand Score: ");
totalScore = new JLabel("Total Score: ");
deal = new JButton("Deal");
deal.addActionListener(this);
secondDeal = new JButton("Second Deal");
secondDeal.addActionListener(this);
buttonPanel.add(deal);
buttonPanel.add(secondDeal);
cpane.add(buttonPanel, BorderLayout.WEST);
cpane.add(scorePanel, BorderLayout.NORTH);
cpane.add(cardPanel, BorderLayout.CENTER);
return cpane;
}
public void actionPerformed(ActionEvent e)
{
Object objectUsed = e.getSource();
if(objectUsed instanceof JButton)
{
if(e.getSource() == deal)
{
doDeal();
}
}
}
private void doDeal()
{
hand = new CardSet();
hand.deal();
repaint();
}
}
CardPanel.java
(This is a subclass of JPanel specific for the cards including the images been loaded and picking out from sprite sheet)
package pokerpkg;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.net.URL;
import javax.swing.JPanel;
public class CardPanel extends JPanel {
Image cardImage;
CardSet hand;
public CardPanel()
{
loadImage();
setBackground(Color.green);
setPreferredSize(new Dimension(15+4*(15+79), 185));
}
public void paint(Graphics g)
{
super.paint(g);
if(cardImage == null)
{
g.drawString("Error: Can't get card images!", 10,30);
return;
}
for(int i =0; i<4; i++)
{
drawCard(g, hand.getCard(i),15 + i * (15+79), 15);
}
}
public void drawCard(Graphics g, Card card, int x, int y)
{
int cx;
int cy;
if(card == null)
{
cy = 4*123; // Cords for a face-down card.
cx = 2*79;
}
else
{
cx = (card.getValue()-1)*79;
switch(card.getSuit())
{
case Card.CLUBS:
cy = 0;
break;
case Card.DIAMONDS:
cy = 123;
break;
case Card.HEARTS:
cy = 2*123;
break;
case Card.SPADES:
cy = 3*123;
break;
default:
cy = 3*123;
}
}
g.drawImage(cardImage, x, y, x+79, y+123, cx, cy, cx+79, cy+123, this);
}
private void loadImage()
{
ClassLoader c1 = CardGame.class.getClassLoader();
URL imageURL = c1.getResource("cards.png");
if(imageURL != null)
{
cardImage = Toolkit.getDefaultToolkit().createImage(imageURL);
}
}
}
Console error
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at pokerpkg.CardSet.deal(CardSet.java:23)
at pokerpkg.CardGame.doDeal(CardGame.java:80)
at pokerpkg.CardGame.actionPerformed(CardGame.java:70)
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.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)
Sorry for no comments in the last two classes just been deleted and re-writing alot trying to get this to work.
Cheers for any help