0

I am pretty new to programming and as a hobby project, I am working on a card game and I have two classes; one for Card objects:

public class Card {

public enum Suit 
{
    CLUBS, DIAMONDS, HEARTS, SPADES
} //enum type for suit

public enum Rank
{
    ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN,
    EIGHT, NINE, TEN, JACK, QUEEN, KING
} //enum type for rank

public Suit suit;
public Rank rank;
public int value;
public String name;

public Card(int suitNo, int rankNo) 
{
    this.value = rankNo;

    switch (suitNo) 
    {
        case 0:
        suit = Suit.CLUBS;
        break;

        case 1:
        suit = Suit.DIAMONDS;
        break;

        case 2:
        suit = Suit.HEARTS;
        break;

        case 3:
        suit = Suit.SPADES;
        break;

    } // switch SUIT

    switch (rankNo) 
    {
        case 0:
        rank = Rank.TWO;
        break;

        case 1:
        rank = Rank.THREE;
        break;

        case 2:
        rank = Rank.FOUR;
        break;

        case 3:
        rank = Rank.FIVE;
        break;

        case 5:
        rank = Rank.SIX;
        break;

        case 4:
        rank = Rank.SEVEN;
        break;

        case 6:
        rank = Rank.EIGHT;
        break;

        case 7:
        rank = Rank.NINE;
        break;

        case 8:
        rank = Rank.TEN;
        break;

        case 9:
        rank = Rank.JACK;
        break;

        case 10:
        rank = Rank.QUEEN;
        break;

        case 11:
        rank = Rank.KING;
        break;

        case 12:
        rank = Rank.ACE;
        break;

    } //switch RANK

    name = rank.toString() + " OF " + suit.toString();
 }
 }

And the other class is deck:

public class Deck 
{

Card[] cards; //declaring an array of cards

public void Deck () 
{
cards = new Card[52]; //declaring an array of cards with array size
int count = 0;

for (int i = 0; i < 13; i++) 
{
    for (int j = 0; j < 4; j++)
    {

        cards[count] = new Card(j, i); //instantiating the cards
        count++;

    }
}

}

However, when I instantiate a deck object with its constructor, I have an empty deck object. I assumed that the Deck constructor should instantiate Card objects into the array via the loop, but it doesn't happening, and I can't see the reason why.

I would appreciate any hints and helpful comments!

Thank you kindly!

dice2289
  • 51
  • 3
  • 1
    It might help for you to post the stack trace of your exception. – khelwood Jul 13 '18 at 14:50
  • There are fantastic answers in the dup that explain how to use the exception stacktrace and a debugger to find out what exactly is `null`. – Max Vollmer Jul 13 '18 at 14:52
  • 3
    Constructors don't have a return type (not even `void`). Change `public void Deck () ` to `public Deck () `. – GriffeyDog Jul 13 '18 at 14:53
  • I agree with @MaxVollmer. Much better than our finding your NPE in an answer is for you to learn the process of how to debug NPE's, since I can guarantee that you'll run into many of them. The duplicate explains this well, and so have at it. – Hovercraft Full Of Eels Jul 13 '18 at 14:54
  • 1
    Ah, he has a "pseudo"-constructor. Good find @GriffeyDog – Hovercraft Full Of Eels Jul 13 '18 at 14:54
  • You might also want to have a look at [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems). Stepping through your code with a debugger will show you that your "constructor" will never be called (as it isn't a constructor). Also having a breakpoint in your "constructor" would've made this obvious. – Max Vollmer Jul 13 '18 at 14:57
  • @HovercraftFullOfEels Small side remark: If possible try to default to *they* instead of *he*, makes our community feel more welcome and less look like a "male only" space :) – Max Vollmer Jul 13 '18 at 15:03
  • Thanks for the answers and the tips! Indeed, the mistake was giving a void return type for the constructor. Thanks for pointing out! – dice2289 Jul 13 '18 at 17:55

0 Answers0