0

So I am building up to a card game but, but I am having some trouble building the deck of cards and OOP in general so I was wondering if I could get some help. This is what I have so far.

public class Card {
    String[] rank = {"Ace", "2", "3", "4", "5", "6",
            "7", "8", "9", "10", "Jack", "Queen", "King"};
    String suit;
    int value;
    String color;

    public Card(String suit, int value) {
        this.suit = suit;
        this.value = value;
        if (suit.equals("Hearts") || suit.equals("Diamonds")) {
            color = "red";
        } else {
            color = "black";
        }
    }

    public int getValue() {
        return value;
    }

    public String getColor() {
        return color;
    }

    public String getSuit() {
        return suit;

    }

    public String getName() {
        name = rank[(value - 1)];
        return name;

    }

    public String toString() {
        return name + " of " + suit;
    }
}

The class for my deck of cards (so far) is as follows:

    public class Deck {
    String[] suits = {"Hearts", "Diamonds", "Clubs", "Spades"};
    static String[] cards = new String[52];

    public Deck() {
        int index = 0;
        for (int i = 0; i < 4; i++) {
            // For each value of card with this suit.
            for (int j = 1; j <= 13; j++) {
                Card card = new Card(suits[i], j);
                cards[index] += card;
                index += 1;
            }
        }
    }

    public static void all() {
        for (int i = 0; i < cards.length; i++) {
            System.out.println(cards[i]);
        }
    }
}

So my question was when I print out the cards, I get something like

nullnull of Hearts
nullnull of Hearts
nullnull of Hearts
nullnull of Hearts....
nullnull of Diamonds
nullnull of Diamonds
nullnull of Diamonds
nullnull of Diamonds
nullnull of Diamonds... etc.

Why am I getting these nulls? What am I doing wrong? Also, any other tips to improve my code would be greatly appreciated.

ip.java
  • 83
  • 11
pctree
  • 11
  • Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – m0skit0 Oct 18 '20 at 14:32

2 Answers2

1

Your problem is the following line of code from the constructor of class Deck.

cards[index] += card;

You need to remove the +.

An array of objects in java is always initialized such that every element is null. Hence you are appending a card to null.

If you would step through your code with a debugger, you would have discovered that. All good IDEs have a debugger. You should learn to use it.

Note that the code in your question does not compile but I figured out how to fix it since the problems are minor.

For the sake of completeness. Here is your code with my fixes. Compare it to your code.

Class Card

public class Card {
    String[] rank = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
    String suit;
    int value;
    String color;

    public Card(String suit, int value) {
        this.suit = suit;
        this.value = value;
        if (suit.equals("Hearts") || suit.equals("Diamonds")) {
            color = "red";
        }
        else {
            color = "black";
        }
    }

    public int getValue() {
        return value;
    }

    public String getColor() {
        return color;
    }

    public String getSuit() {
        return suit;
    }

    public String getName() {
        String name = rank[(value - 1)];
        return name;

    }

    public String toString() {
        return getName() + " of " + suit;
    }
}

Class Deck
(Note that I added a main() method just for testing purposes.)

public class Deck {
    String[] suits = {"Hearts", "Diamonds", "Clubs", "Spades"};
    Card[] cards = new Card[52];

    public Deck() {
        int index = 0;
        for (int i = 0; i < 4; i++) {
            // For each value of card with this suit.
            for (int j = 1; j <= 13; j++) {
                Card card = new Card(suits[i], j);
                cards[index] = card;
                index += 1;
            }
        }
    }

    public void all() {
        for(int i = 0; i < cards.length; i++) {
            System.out.println(cards[i]);
        }
    }

    public static void main(String[] args) {
        new Deck().all();
    }
}
Abra
  • 19,142
  • 7
  • 29
  • 41
0

Your example does not look complete, as I don't see name field in Card class defined. But what I see is that name is initialized in getName methods. I guess you don't initialize it. Either use getName in toString:

public String toString(){
    return getName() + " of " + suit;
}

or initialize name in the constructor: name = rank[(value - 1)]

Prefer initialization in the constructor, otherwise if you refer to name in some other place you can get the same issue.

ip.java
  • 83
  • 11
  • Thanks for catching that. So when I initialized it in my constructor, this time I get things like "nullAce of Diamonds, null2 of Diamonds.." Why are these cards still preceded by nulls? Am I doing my initializations wrong? Thank you so much for your help by the way. – pctree Oct 18 '20 at 14:50
  • Additionally, I guess you need to update: static String[] cards = new String[52]; Are you sure you need to store strings instead of Card objects? – ip.java Oct 18 '20 at 15:05