0

I'm making a program that takes in an input of lets say: 3H 4C 8S 2D 4H a poker hand with a three of hearts, four of clubs etc.

I have a card class and each card has a rank and suit which are both defined as enums. These are the methods I use in the Card class:

    // METHOD TO GET CARDS IN RANK ORDER
    public static Card[] getHand(String[] args){
        Card[] hand = new Card[5];
        for (Rank r : Rank.values()) {
            int j=0;
            for (int i = 0 ; i < args.length ; i++) {
                    if(r.rankNum().equals(args[i].substring(0,1))){
                            hand[j] = new Card(r, Card.getSuit(args, i));
                            j++;
                    }
             }
        } return hand;
    }

    // METHOD TO GET SUIT
    public  static Suit getSuit(String[] args, int i) {
            for (Suit s : Suit.values()) {
                    if (s.suitCode().equals(args[i].substring(1,2))) {
                            return s;
                    }
            } return null;
    }

In the main I do the following:

// TEST CARD ARRAY AND CARD PRINT
Card[] handTest = new Card[5];
handTest[0] = new Card(Rank.TWO, Suit.CLUBS);
handTest[1] = new Card(Rank.TWO, Suit.CLUBS);
handTest[2] = new Card(Rank.TWO, Suit.CLUBS);
handTest[3] = new Card(Rank.TWO, Suit.CLUBS);
handTest[4] = new Card(Rank.TWO, Suit.CLUBS);
for (int i = 0 ; i < 5 ; i++){
    handTest[i].printCard();
}

// TRY WITH ACTUAL ARGUMENT INPUT (3H 4D 7C 2D 8S)
Card[] hand = new Card[5];
hand = Card.getHand(args);
for (int i = 0 ; i < 5 ; i++){
    hand[i].printCard();
}

I get the following output

Suit: CLUBS   Rank: TWO
Suit: CLUBS   Rank: TWO
Suit: CLUBS   Rank: TWO
Suit: CLUBS   Rank: TWO
Suit: CLUBS   Rank: TWO
Suit: SPADES   Rank: EIGHT
Exception in thread "main" java.lang.NullPointerException
at Poker.main(Poker.java:20)

If I have the printCard method within the innermost loop of getHand, I don't get errors but its after the getHand method has run that I get the null pointer exception.

I have now read some posts explaining null pointer exceptions but I cannot seem to fix this. Line 20 refers to hand[i].printCard();

Alex Lama
  • 9
  • 3
  • If you've done even a little searching on solving a NullPointerException (NPE), you'll know that the most important bit of information that we need is the exception's associated stacktrace and some identification of the line that causes it, something that the stacktrace will tell you, and unfortunately neither of which you've posted here with your question. Please fix this so that we can help you. – Hovercraft Full Of Eels Oct 10 '15 at 02:05
  • You also need to inform us what you're passing in via the command line and how you're passing it in. Also, you need to learn the general concepts of how to debug a NPE (NullPointerException). **You should critically read your exception's stacktrace to find the line of code at fault, the line that throws the exception, and then inspect that line carefully**, find out which variable is null, and then trace back into your code to see why. You will run into these again and again, trust me. – Hovercraft Full Of Eels Oct 10 '15 at 02:06
  • @HovercraftFullOfEels I have tried to rectify my post and make it clearer and read a post about null pointer exceptions but I just can't understand where my null object is. – Alex Lama Oct 10 '15 at 02:15
  • What do you pass into your command line? You look to pass only **one** card data into the command line so only one card in the array is non-null. – Hovercraft Full Of Eels Oct 10 '15 at 02:18
  • And what's this for? `if(r.rankNum().equals(args[i].substring(0,1))){`, I wonder if it's preventing your hand from filling. Do println tests in there or use a debugger to see what r.rankNum(), args[i],... are during this loop. And why use a fixed size array and not a more flexible ArrayList? – Hovercraft Full Of Eels Oct 10 '15 at 02:20
  • Some suggestions: There seems to be a problem with setting up the `Card` array because you're not initializing each element of the array in `getHand()`. You could simplify the code like this: implement lookup methods in the enums, e.g. `public static Rank fromRankNum(String rankNum)` and `public static Suit fromSuitCode(String suitCode)` an implement the for-loops there. Then, in your `main` method you only need to parse each element of `String[] args` so that the substring corresponding to the first char of `args[i]` is passed to `Rank.fromRankNum()` and the second to `Suit.fromSuitCode()`. – Mick Mnemonic Oct 10 '15 at 02:21
  • @HovercraftFullOfEels I pass `3H 4D 7C 2D 8S` into the command line which is 5 string elements in the `args` array each representing a card. – Alex Lama Oct 10 '15 at 03:15
  • @HovercraftFullOfEels That line says when the input rank is equal to the enum rank then go ahead an make a new card with that rank... and since the enum is defined in rank order, the cards are made in rank order. – Alex Lama Oct 10 '15 at 03:16
  • @MickMnemonic I see your point but then I don't think that would let me create a card array in order of rank.. which I need to be able to tell when a player has a straight (7,8,9,10) etc. – Alex Lama Oct 10 '15 at 03:18
  • 1
    If ordering of cards is important for the application logic, then I suggest you make `Card` implement `Comparable` and use `Arrays.sort` for ordering the array (or even better, use a dynamic data structure such as `TreeList` instead of an array). In other words: separate the concers of 1) initializing the poker hand and 2) ordering the hand, in order to simplify your code. – Mick Mnemonic Oct 10 '15 at 03:27
  • @MickMnemonic So I did try sorting the array at first however I wasn't able to figure out how to sort them by rank... I will look into `comparable` and `TreeList` thank you – Alex Lama Oct 10 '15 at 03:40
  • You have a major problem with your `int j` variable. You're initializing it to 0 inside of your for loop and so it re-sets to 0 each time. You only set the first array element because of this. This, `int j=0;` must go **before** your for loop. Note: this was solved by debugging your code -- something you should do be fore coming here. All it takes is use of a debugger or println statements. – Hovercraft Full Of Eels Oct 10 '15 at 04:07
  • @HovercraftFullOfEels I cannot thank you enough... That was really stupid of me. – Alex Lama Oct 10 '15 at 04:09
  • Note that the more robust solution is as @MickMnemonic presents above -- use the right collection for the job. – Hovercraft Full Of Eels Oct 10 '15 at 04:11

0 Answers0