0

I want to create a deck of cards(object), and it shows nullnullnull... however i construct the card.

This is the class I use to initialize the deck:

import black.jack.bean.Card;

public class InitDeck {
    
    public InitDeck() {
        
    }
    
    public void createDeck(Card[] cards) {
        for(int idx = 0; idx<cards.length; idx++) {
            int contValueNum = 1;
            boolean valueYes = false;
            
            cards[idx] = new Card((char) idx, idx);
            
            if(!valueYes) {
                switch(contValueNum) {
                    case 1:
                        cards[idx].setKey('A');
                        cards[idx].setKeyValue(contValueNum);
                    break;
                    case 11:
                        cards[idx].setKey('J');
                        cards[idx].setKeyValue(contValueNum);
                    break;
                    case 12:
                        cards[idx].setKey('Q');
                        cards[idx].setKeyValue(contValueNum);
                    break;
                    case 13:
                        cards[idx].setKey('K');
                        cards[idx].setKeyValue(contValueNum);
                    break;
                }
                
                if(idx==cards.length) {
                    valueYes = false;
                }
            }
        }
    }
    
    public void visDeck(Card[] cards) {
        for(int idx = 0; idx<cards.length; idx++) {
            System.out.print(cards[idx]);
        }
    }
}

This is the class Deck and the class Card

public class Deck {
    
    private static final int CARDS = 52;
    
    //private char[] deck = new char[CARDS];
    protected Card[] cards = new Card[CARDS];
    
    public Deck() {
        
    }

    public Card[] getCards() {
        return cards;
    }

    public void setCards(Card[] cards) {
        this.cards = cards;
    }
}
public class Card {

    public char key;
    public int  keyValue;
    
    public Card(char key, int keyValue) {
        this.key      = key;
        this.keyValue = keyValue;
    }

    public Card() {
        
    }
    
    public char getKey() {
        return key;
    }

    public void setKey(char key) {
        this.key = key;
    }

    public int getKeyValue() {
        return keyValue;
    }

    public void setKeyValue(int keyValue) {
        this.keyValue = keyValue;
    }
}

And this is my Console

public class Console {
    
    public Console() {
        
    }

    public void execute() {
        InitDeck initDeck= new InitDeck();
        Deck deck = new Deck();
        Card card = new Card();
        
        initDeck.visDeck(deck.getCards());
    }

}

And the result is:

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull

There's someone who could help me out?

geocodezip
  • 158,664
  • 13
  • 220
  • 245
ToTosty
  • 36
  • 5
  • 3
    We create a new deck through `Deck deck = new Deck();`. In class `Deck`,we can see that an `Card[] cards` is initialized with size `CARDS` (`=52`). When the array is created, all fields in the array are initialized with `null`. Hence, if we print the fields of the array, all fields are `null`. In order to have the array filled, we need to write values to the array (probably by calling `initDeck.createDeck(deck.getCards());`) – Turing85 Jun 05 '21 at 11:29
  • It still doesn't work, now the value is not null, but it shows (black.jack.bean.Card@5d22bbb7), why? – ToTosty Jun 06 '21 at 10:50
  • Please read: [Can I ask only one question per post?](https://meta.stackexchange.com/questions/222735/can-i-ask-only-one-question-per-post) --- Please read: [How do I print my Java object without getting “SomeType@2f92e0f4”?](https://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4) – Turing85 Jun 06 '21 at 11:12
  • You don't call `createDeck` anywhere, call it from `Deck` constructor – Jean-Baptiste Yunès Jun 06 '21 at 17:37

0 Answers0