Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Apr 2011
    Location
    Perth, Western Australia
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Run time error popping up for my FullDeck arrary class

    Hi

    I created two classes. one Card class which basically specifies what type of data a Card object would have (Suit values and suit numbers). The other one is the Full Deck class which is basically meant to create 52 Card objects (which consist of all 52 cards in a Full Deck). I have used arrays in Full Deck.

    BOTH CLASSES COMPILE FINE. WHEN I RUN THE APPLICATION (FULLDECK), it STOPS EXECUTING DUE TO A RUN TIME ERROR! I am a beginner in Java, so I am trying to figure out whats the cause.

    Can someone please tell what the cause for the runtime error is and what can be done to correct it?

    The Card class is as follows
    Code:
    //Importing JOptionPane
    import javax.swing.JOptionPane;
    
    //Class Declaration
    public class Card
    {
    	//Declaration of variables
    	private String suitValue = new String();
    	private String rankValue = new String();
    	
    	//getter method for the Suit
    	public String getSuit()
    	{
    		return suitValue;
    	}
    	
    	//getter method for the Rank
    	public String getRank()
    	{
    		return rankValue;
    	}
    	
    	//setter method for the Suit
    	public void setSuit(int suit)
    	{
    		switch(suit)
    		{
    			case 1: suitValue= "Hearts";
    							break;
    			case 2: suitValue="Diamonds";
    							break;
    			case 3: suitValue="Spades";
    							break;
    			case 4: suitValue="Clubs";
    							break;
    			
    			//If this displays, it means there is a bug in this program
    			default: JOptionPane.showMessageDialog(null, "Hmmm... This seems to be an invalid value");
    
    		}
    
    	}
    	
    		
    	//setter method for the Rank
    	public void setNumbers(int rank)
    	{
    		switch(rank)
    		{
    				case 1: rankValue="Ace";
    						break;
    				case 2: rankValue="2";
    						break;
    				case 3: rankValue="3";
    						break;
    				case 4: rankValue="4";
    						break;
    				case 5: rankValue="5";
    						break;
    				case 6: rankValue="6";
    						break;
    				case 7: rankValue="7";
    						break;
    				case 8: rankValue="8";
    						break;
    				case 9: rankValue="9";
    						break;
    				case 10: rankValue="10";
    						break;
    				case 11: rankValue="Jack";
    						break;
    				case 12: rankValue="Queen";
    						break;
    				case 13: rankValue="King";
    						break;
    				
    				
    				//If this displays, it means there is a bug in this program
    				default: JOptionPane.showMessageDialog(null, "Hmmm... This seems to be an invalid value");
    			
    			
    			}
    	}
    	
    
    }
    The FullDeck class is as follows:
    Code:
    //Importing JOptionPane
    import javax.swing.JOptionPane;
    
    //Class Declaration
    public class FullDeck
    
    {
    	//Main Funtion
    	public static void main(String[] args)
    	{
    	
    		//Declaration of variables
    		int suit, suit2;
    		String userCard = new String();
    		String computerCard = new String();
    		final int CARDS_IN_SUIT = 13;
    		final int RANDOM_SUIT = 4;
    
    		Card[] cardDeck = new Card[52];
    		
    		int x;
    		
    		for(x=0;x<52;++x)
    		{
    			for(suit=1;suit<=13;++suit)
    			{
    				cardDeck[x].setSuit(suit);
    			
    				for(int value=1;value<=CARDS_IN_SUIT;++value)
    				cardDeck[x].setNumbers(suit);
    				}
    
    		}
    		
    		for(int y=0;y<52;++y)
    		System.out.println(cardDeck[y].getSuit() + cardDeck[y].getRank());
    
    		
    	}
    
    }

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    You have specified that cardDeck is a Card[], but you have not instantiated it to anything. Each offset in cardDeck is therefore a null, so you'll be pulling up a NullPointerException on this line: cardDeck[x].setSuit(suit);. You need to invoke a new Card() first and write it to cardDeck[x].

    I don't like the iteration's either. Consider using two of simply numFaces and numSuits (13 and 4 in a standard deck ignoring joker). So I'd suggest 2 loops nested of the number of faces and suits, and then keep a separate incremented variable for adding. I'd also suggest adding two constructors to card, one default and one that accepts the face and the suit. Chain them together.

    Is this for an assignment? If not, I'd also suggest using enums and collections. Any collection will do, but if you treat the deck as a stack or queue, I'd suggest a linkedlist or subclass of such (using deque interface) would be a good idea. Collections also have an added benefit of the shuffle method.

  • Users who have thanked Fou-Lu for this post:

    trev777 (10-20-2012)

  • #3
    New Coder
    Join Date
    Apr 2011
    Location
    Perth, Western Australia
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks for your help Fou-Lou. Your a legend! I understood your first two paragraphs. That helped me correct my code. Yes it was for an assignment.

  • #4
    New Coder
    Join Date
    Apr 2011
    Location
    Perth, Western Australia
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    corrected source code.Game Zone 17a Chapter 8- Java Programming 6th ed Joyce Farrell

    NOTE: The source code has been posted for learning purposes for new programmers like me. Please use it for learning purposes and not for other purposes. Do NOT copy CODE. Learn the concepts and write your own.




    Card Class

    Code:
    /*
    Title= Card class
    Author= Trevlyn Farrar
    Date= 27/09/2012
    */
    
    //Importing JOptionPane
    import javax.swing.JOptionPane;
    
    //Class Declaration
    public class Card
    {
    	//Declaration of variables
    	private String suitValue = new String();
    	private String rankValue = new String();
    	private int rankNumber;
    	
    	//getter method for the Suit
    	public String getSuit()
    	{
    		return suitValue;
    	}
    	
    	//getter method for the Rank
    	public String getRank()
    	{
    		return rankValue;
    	}
    	
    	public int getRankNumber()
    	{
    		return rankNumber;
    	}
    	
    	//setter method for the Suit
    	public void setSuit(int suit)
    	{
    		switch(suit)
    		{
    			case 1: suitValue= "Hearts";
    							break;
    			case 2: suitValue="Diamonds";
    							break;
    			case 3: suitValue="Spades";
    							break;
    			case 4: suitValue="Clubs";
    							break;
    			
    			//If this displays, it means there is a bug in this program
    			default: JOptionPane.showMessageDialog(null, "Hmmm... This seems to be an invalid value");
    
    		}
    
    	}
    	
    		
    	//setter method for the Rank
    	public void setNumbers(int rank)
    	{
    		rankNumber=rank;
    		switch(rank)
    		{
    				case 1: rankValue="Ace";
    						break;
    				case 2: rankValue="2";
    						break;
    				case 3: rankValue="3";
    						break;
    				case 4: rankValue="4";
    						break;
    				case 5: rankValue="5";
    						break;
    				case 6: rankValue="6";
    						break;
    				case 7: rankValue="7";
    						break;
    				case 8: rankValue="8";
    						break;
    				case 9: rankValue="9";
    						break;
    				case 10: rankValue="10";
    						break;
    				case 11: rankValue="Jack";
    						break;
    				case 12: rankValue="Queen";
    						break;
    				case 13: rankValue="King";
    						break;
    				
    				
    				//If this displays, it means there is a bug in this program
    				default: JOptionPane.showMessageDialog(null, "Hmmm... This seems to be an invalid value");
    			
    			
    			}
    	}
    	
    
    }
    FullDeck Class

    Code:
    /*
    Title= Full Deck class
    Author= Trevlyn Farrar
    Date= 13/10/2012
    */
    
    //Importing JOptionPane
    import javax.swing.JOptionPane;
    
    //Class Declaration
    public class FullDeck
    
    {
    	//Main Funtion
    	public static void main(String[] args)
    	{
    	
    		//Declaration of constants
    		final int CARD_DECK = 52;
    		final int CARDS_IN_SUIT = 13;
    		final int TYPES_OF_SUIT = 4;
    
    
    		//Creating the array for 52 Card objects for the deck
    		Card[] cardDeck = new Card[52];
    		
    		
    		//Looping and asssigning suit and card values to all 52 Card objects in the card Deck
    		for(int value=1;value<=CARDS_IN_SUIT;value++)
    		{
    			for(int suit=1;suit<=TYPES_OF_SUIT;suit++)
    			{
    				int x=((value-1)*4)+(suit-1);
    				cardDeck[x] = new Card();
    				cardDeck[x].setSuit(suit);
    				cardDeck[x].setNumbers(value);
    			}
    		}
    		
    		
    		//Displaying the 52 Cards in the Deck
    		for(int x=0;x<CARD_DECK;x++)
    		{
    			System.out.println(cardDeck[x].getRank() + " of " + cardDeck[x].getSuit());
    		}
    	}
    
    }


  •  

    Posting Permissions

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