-1

Sorry if I am coming to you with such a basic issue. I tried searching(and searching some more) but I cannot find any solution. I am still a newbie to programming, I am trying to create a java version of the card game WAR. I have been troubleshooting this for over a week and have changed and tried many things, but nothing seems to solve my issue. I feel that my logic is what is causing the problem. My App class(where I am getting the error is on the deck.draw line) looks like this:

package warCardGame;

public class App {

    public static void main(String[] args) {
    
        
        
        Deck deck = new Deck();
        deck.shuffle();
        
        Player p1 = new Player("Harvey");
        Player p2 = new Player("Dee");
        
        for(int i = 1; i < 26; i++) {
            p1.hand.add(deck.draw());
            p2.hand.add(deck.draw());
        }


        for(int i = 0; i < 26; i++) {
            Card p1Card = p1.flip();
            Card p2Card = p2.flip();
            
            if(p1Card.getValue() > p2Card.getValue()) {
                p1.incrementScore();
            } else if(p2Card.getValue() > p1Card.getValue()) {
                p2.incrementScore();
            } else {
                System.out.println("A rematch is needed!");
            }
        }
        if(p1.score > p2.score) {
            System.out.println("The final score for " + p1.name +" is" + p1.score + "\n");
            System.out.println("The final score for " + p2.name +" is" + p2.score + "\n");
            System.out.println("The winner today is " + p1.name + " !");
        }else if(p2.score > p1.score) {
            System.out.println("The final score for " + p2.name +" is" + p2.score + "\n");
            System.out.println("The final score for " + p1.name +" is" + p1.score + "\n");
            System.out.println("The winner today is " + p2.name + " !" + "\n");
        }else {
            System.out.println("The final score is a DRAW! A rematch is needed!");
        }

    }

}

My card class looks like this:

package warCardGame;

public class Card {

     public static final String[] SUIT = {"Clubs", "Diamonds", "Hearts", "Spades"};
     public static final String[] RANK = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"};
     

    private static int name;
    private static int value;
    
    
    public void describe() {
        for(int i = 0; i < SUIT.length - 1; i++) {
            for(int j = 0; j < RANK.length - 1; j++) {
                System.out.println("The current suit is " + SUIT + " " + ", and the current rank is " + RANK + " .");
            }
        }
    }
    //method to assign int values to the SUIT string array
    public static int defineSuitValue() {
        value = 0;
        for(int i = 0; i <= SUIT.length; i++) {
            value = value + 1;
        }
        return value;
    }
    //method to assign int values to the RANK string array
    public static int defineRankValue() {
        name = 1;
        for(int i = 0; i <= RANK.length; i++) {
            name = name + 1;
        }
        return name;
    }
    
    public Card(int name, int value) {
        Card.name = defineRankValue();
        Card.value = defineSuitValue();
    }
    
    
    public int getName() {
        return defineSuitValue();
    }
    public void setName(int name) {
        Card.name = defineSuitValue();
    }
    public int getValue() {
        return defineRankValue();
    }
    public void setValue(int value) {
        Card.value = defineRankValue();
    }
    
    
}

The deck class:

package warCardGame;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Deck {
    List<Card> cards = new ArrayList<Card>();
    
    Card[] deck = new Card[52];
    
    
    public Deck() {
        //int c = 0;
        for(int i = 0; i < 4; i++) {
            for(int j = 2; j <= 14; j++) {
                cards.add(new Card(i, j)); //need to rework
            }
        }
        
    }
    public List<Card> getCards(){
        return cards;
    }
    
    public void setCard(List<Card> cards) {
        this.cards = cards;
    }
    
    public void shuffle() {
        Collections.shuffle(cards);
    }

    public Card draw() {
        if(cards.isEmpty()) {
            return null;
        }else {
            Card topCard = cards.get(0);
            cards.remove(0);
            return topCard;
        }
    }
}

My player class:

package warCardGame;

import java.util.List;

public class Player {
    
    List<Card> hand;
    int score;
    String name;
    
    public Player(String name) {
        this.score = 0;
        this.name = name;
}
    public void describe() {
        System.out.println(name + " " + "has a score of " + score + " " + "!");
            for(Card card : hand) {
                card.describe();
            }
    }
    
    public List<Card> getHand() {
        return hand;
    }
    public void setHand(List<Card> hand) {
        this.hand = hand;
    }
    public int getScore() {
        return score;
    }
    public void setScore(int score) {
        this.score = score;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Card flip() {
        Card topCard = hand.get(0);
        hand.remove(0);
        return topCard;
    }
    
    public void draw(Deck deck) {
        Card card = deck.draw();
        hand.add(card);     
    }
    
    public void incrementScore() {
        score = getScore() + 1;
    }
}

I have spent so many days trying to fix this, and I am not sure if I have made it worse by adding additional code. I TRULY appreciate any feedback you can provide, thanks!!

JarveyD
  • 1
  • 2
  • when is the null pointer exception happening? an can you also post the stacktrace of the exception – Bentaye Jul 08 '22 at 12:10
  • In `Player` class you need to initialize `hand` attribute: `List hand = new ArrayList();` – Yannis Sauzeau Jul 08 '22 at 12:13
  • 1
    You need to understand what a `NullPointerException` is and what the stacktrace is telling you. Then you will immediatly see the candidates for that Exception in the line `p1.hand.add(deck.draw());`. – f1sh Jul 08 '22 at 12:13
  • The error is very clear "Cannot invoke "java.util.List.add(Object)" because "p1.hand" is null". You never initialize `hand` – Ivo Jul 08 '22 at 12:16
  • The error I am getting is this: Exception in thread "main" java.lang.NullPointerException at warCardGame.App.main(App.java:16) – JarveyD Jul 08 '22 at 12:16
  • 1
    Other oddities: this loop iterates 25 times: `for(int i = 1; i < 26; i++)` (the "dealing" loop) and the following loop iterates 26 times: `for(int i = 0; i < 26; i++)` (the "playing" loop). – Computable Jul 08 '22 at 12:17

1 Answers1

0

As far as i can see you forgot to initialize "hand".

List<Card> hand;

You are creating two new players. In the constructor of the player you are initializing the score and the name.

public Player(String name) {
    this.score = 0;
    this.name = name;
}

The hand is still not initialized at this moment. If you call the "add()" method on the not-initialized hand-list it throws the null-pointer because the not initialized list is null.

Player p1 = new Player("Harvey");
Player p2 = new Player("Dee");
    
for(int i = 1; i < 26; i++) {
    p1.hand.add(deck.draw());
    p2.hand.add(deck.draw());
}

You need to initialize arrays or lists with empty arrays/lists. To initialize the list you should do something like:

List<Card> hand = new ArrayList<Card>();

For further information have a look at this question: How to initialize List<String> object in Java?

Smirgal
  • 46
  • 2