Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Help with Card Game

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Help with Card Game

    Hi guys,

    this is my first time posting here so please go easy on me. So I'm trying to assign a suit, number, and value for the cards for a Black Jack game for my Computer Programming class but I'm running into a problem. I have variables in my "Card.class" for all value, number, and suit but it just doesn't seem to work. It compiles fine but when I run the applet, I get an error saying something along the lines of: NullPointerException at line 64. Which is the line, "Cards[i].suit = "Spade";" . I'll appreciate any help.




    // Import section
    // Use this section to add additional libaries for use in your program.
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.lang.Math;
     
     
     
    // This begins the class definition.
    // Notice that this is a "world".  You can tell since it extends Applet.
    public class BlackJack extends Applet implements Runnable, /*KeyListener,*/ MouseMotionListener, MouseListener
     
    {
    	//variable declaration section
    	// public  datatype  variablename
     
    	public int xTitle;
    	public int yTitle;
    	public Card[] Cards;
    	public int sizeC;
    	public int randomXpos, randomYpos, randomCard;
    	public Image cardDeck;
     
     
     
     
     
    	Graphics bufferGraphics;
    	Image offscreen;
     
     
    	//Sets up a Thread called thread
    	Thread thread;
     
    	// Method definition section
     
    	// init() is the first method an Applet runs when started
    	public void init()
    	{
    		//Initialize variables
    		xTitle=250;
    		yTitle=50;
    		sizeC = 52;
     
    		Cards = new Card[sizeC];
    		/*for(int j = 0; j<13; j++)
    		{
    			for(int i = j; i<52; i = i+13)
    			{
    				Cards[i].value = j+1;
    				Cards[i].number = j+1;
    			}
    		}*/
     
    		for(int i = 0; i < 13; i++)
    		{
    			Cards[i].suit = "Spade";
    		}
    		for(int i = 13; i < 26; i++)
    		{
    			Cards[i].suit = "Club";
    		}
    		for(int i = 26; i < 39; i++)
    		{
    			Cards[i].suit = "Diamond";
    		}
    		for(int i = 39; i < 52; i++)
    		{
    			Cards[i].suit = "Heart";
    		}
     
    		cardDeck = getImage(getDocumentBase(), "CardDeck.gif");
     
     
     
    		//double buffering
    		offscreen = createImage(800,600); //create a new image that's the size of the applet
    		bufferGraphics = offscreen.getGraphics(); //set bufferGraphics to the graphics of the offscreen image.
     
    		setSize(800, 600);
     
    		addMouseListener(this);
    		addMouseMotionListener(this);
    		//Set up the thread
    		thread = new Thread(this);  //constructs a new thread
    		thread.start();				//starts the thread
    	}//init()
    Last edited by JSeol14; April 21st, 2012 at 10:38 PM.


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Help with Card Game

    Here's an idea, though I don't have the Card.java code.

    for(int i = 0; i < 13; i++)
    		{
                             try{
     
    			Cards[i].suit = "Spade";
    }
     
     
    catch(NullPointerException npe)
    {
    System.out.println("Card " + i  + " is null!");
    }
    		}
    That would detect where it's going Null. However, I think I see the problem.

    However, it would seem to me that the problem is that you define a Card array but never initialize it with something like
    for (int i =0; i < Cards.length; i++)
    {
    Cards[i]= new Card();
    }

    If it's not initalized, then the entire Card there is null, hence you can't go referring to .value and stuff of a null Card.

  3. The Following User Says Thank You to javapenguin For This Useful Post:

    JSeol14 (April 21st, 2012)

  4. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with Card Game

    Wow thank you so much. I can't believe I overlooked that so easily. I feel pretty stupid now. But thank you again!

Similar Threads

  1. A Card Game
    By Paytheprice in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 25th, 2012, 07:30 AM
  2. Card Game Problem.
    By d'fitz in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 5th, 2011, 07:30 AM
  3. Card Game help....
    By macFs89H in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 2nd, 2011, 07:55 AM
  4. Card Game Problem....Need Help
    By macFs89H in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 28th, 2011, 07:30 AM
  5. help on war card game project
    By sc0field1 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 3rd, 2011, 04:51 AM