Question: Error in Java project Console Output: Initial draw: 9?, 8?, 6?, 2?, Q?, Hand (before draw): 2?, 6?, 8?, 9?, Q? Exception in thread main

Error in Java project

Console Output:

Initial draw: 9?, 8?, 6?, 2?, Q?,

Hand (before draw): 2?, 6?, 8?, 9?, Q?

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "edu.unca.csci202.Card.compareTo(edu.unca.csci202.Card)" because the return value of "java.util.Stack.peek()" is null

at edu.unca.csci202.Player.takeTurn(Player.java:44)

at edu.unca.csci202.CardGame.run(CardGame.java:107)

at edu.unca.csci202.CardGame.main(CardGame.java:38)

Card.java

Error in Java project Console Output: Initial draw: 9?, 8?, 6?, 2?,

HandOfCards.java

Q?, Hand (before draw): 2?, 6?, 8?, 9?, Q? Exception in thread

CardGame.java

package edu.unca.csci202; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.List; import java.util.Queue; import java.util.Random; import java.util.Stack; /** * DO NOT MODIFY THIS FILE * * Class that defines the card game */ public class CardGame { private Queue drawPile; private Stack discardPile; public static void main(String[] args) { // ==== Testing ==== Card c1 = new PlayingCard(1,"A" + "\u2660"); System.out.println(c1); // This should print: A Card c2 = new PlayingCard(2,"2" + "\u2661"); System.out.println(c2); // This should print: 2 HandOfCards h = new CardGameHand(); h.add(c1); h.add(c2); System.out.println(h); // This should print: 2, A // ==== Play the game ==== Player[] players = new Player[2]; players[0] = new Player("Player 1"); players[1] = new Player("Player 2"); CardGame game = new CardGame(); game.run(players); } public CardGame() { drawPile = new ArrayDeque(); discardPile = new Stack(); } public void shuffleDeck() { System.out.println("======== Shuffling Deck ========"); drawPile.clear(); discardPile.clear(); List unshuffled = new ArrayList(); //String[] suits = {"Spades","Hearts","Diamonds","Clubs"}; String[] suits = {"\u2660","\u2661","\u2662","\u2663"}; String[] values = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"}; for(int v=0; v 0) { int index = rand.nextInt(unshuffled.size()); Card card = unshuffled.remove(index); drawPile.add(card); } System.out.println("Shuffled["+ drawPile.size() +"]: " + drawPile); } public void run(Player[] players) { int round = 0; shuffleDeck(); while(true) { System.out.println("======== Round " + round + " ========"); for(int i=0; i 0) { System.out.print(", " + discardPile.peek() + " on top"); } System.out.println(); if(round == 0) { players[i].drawInitalHand(drawPile); } players[i].takeTurn(drawPile, discardPile); if(players[i].hasWon()) { System.out.println("Winner: "+ players[i]);

return; } } // End of round round++; }

}

} Player.java

package edu.unca.csci202; import java.util.Queue; import java.util.Stack; /** * DO NOT MODIFY THIS FILE * * Class that defines the card player */ public class Player { private int numberOfTurnsTaken; private HandOfCards hand; private String name; public Player(String name) { //=========================================== // TODO: CardGameHand and PlayingCard must be implemented hand = new CardGameHand(); //=========================================== numberOfTurnsTaken = 0; this.name = name; } public String toString() { return name; } public void drawInitalHand(Queue drawPile) { System.out.print("Initial draw: "); for(int i=0;i drawPile, Stack discardPile){ numberOfTurnsTaken++; System.out.println("Hand (before draw): "+ hand); boolean cardDrawn = false; if(discardPile.size() > 0) { Card lowest = hand.peekSmallest(); if(discardPile.peek().compareTo(lowest) > 0) { Card drawn = discardPile.pop(); System.out.println("\tDrew "+drawn+" from discard pile"); hand.add(drawn); cardDrawn = true; } } // If a card was not drawn from the discard pile if(!cardDrawn) { Card drawn = drawPile.remove(); System.out.println("\tDrew "+drawn+" from draw pile"); hand.add(drawn); } System.out.println("Hand (after draw): "+ hand); // discard Card discard = hand.removeSmallest(); System.out.println("Discarded "+discard); discardPile.push(discard); System.out.println("Hand (after discard): "+ hand); } public boolean hasWon() { int score = hand.getTotalScore(); System.out.println(this + "'s score is "+ score); if(score >= 50) { return true; } return false; }

} CardGameHand.java

"main" java.lang.NullPointerException: Cannot invoke "edu.unca.csci202.Card.compareTo(edu.unca.csci202.Card)" because the return value of "java.util.Stack.peek()" is

PlayingCard.java

null at edu.unca.csci202.Player.takeTurn(Player.java:44) at edu.unca.csci202.CardGame.run(CardGame.java:107) at edu.unca.csci202.CardGame.main(CardGame.java:38) Card.java HandOfCards.java CardGame.java package edu.unca.csci202;

kage edu.unca.csci202; DO NOT MODIFY THIS FILE Interface to define a hand of cards in the card game lic interface Handofcards T extends card >{ /8 * Add a card to the internal data structures * of the class * Eparam card to be added 8/ public void add(T card); /8 * Return the smallest value card in the hand * ereturn smallest value Card / public T peeksmallest(); * Return and remove smallest value card in the hand * ereturn smallest value Card 8/ public T removesmallest(); /8 * Get the sum of all the points from all the Cards in the hand * Ereturn sum of points of cards in the hand / public int gettotalscore(); /8 * Get a comma separated list of the display * values of all the the Cards in the hand * IMPORANT: this list MUST return sorted * high to low. Face cards/10's must be first, * and aces should be last. * Ereturn / public string tostring(); package edu.unca.csci202; public class PlayingCard implements Card \{ private int points; private String display; public PlayingCard(int points, String display) this.points = points; this.display = display; public int getPoints()\{ return points; public String getDisplay() \{ return display; public String toString() \{ return display; public int compareTo(Card other) \{ return this.getPoints()- other.getPoints()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!