-1

I just started learning Java required for my course. Everything seems fine, no syntax errors but when I run my code I have an error

    Exception in thread "main" java.lang.NullPointerException
    at Deck.<init>(Deck.java:18)
    at MainDriver.main(MainDriver.java:17)

Here is my code.

Class Card

    public abstract class Card {

    public CardValue value;
    public CardSuit suit;

    CardValue [] cardvalue = CardValue.values();
    CardSuit [] cardsuit = CardSuit.values(); 

    public Card () {
        value = cardvalue[0];
        suit = cardsuit[0]; 
    }

    public String toString() {
        return this.suit + " of " + this.value;
    }

    abstract boolean CardCompare(Card P1, Card P2);
    }

Class Deck

import java.util.Random;

public class Deck  {

    Card[] playingCards = new Card[52];

    public Deck() {

        int cardNumber = 0;

        CardValue [] cardvalue = CardValue.values();
        CardSuit [] cardsuit = CardSuit.values(); 

        for (int i = 0; i < 4; i++)
        {
            for(int j = 0; j < 13; j++)    //Error here (Deck.java:18)
            {
                playingCards[cardNumber].value = cardvalue[j];
                cardNumber++;
            }
            playingCards[cardNumber].suit = cardsuit[i];
        }
    }   

    public Card draw() {

        Random rand = new Random();
        int cardDraw = rand.nextInt(52);

        return playingCards[cardDraw];

    }
}

Class Main

public class MainDriver extends Card{

    static final int HANDS = 52;

    boolean CardCompare(Card P1, Card P2)
    {
        if (P1.value.ordinal() > P2.value.ordinal())
            return true;
        else if (P1.suit.ordinal() > P2.suit.ordinal())
            return true;
        else return false;
    }

    public static void main(String[] args) {

        Deck player1 = new Deck();    //Some reason there's a error here too (MainDriver.java:17)
        Deck player2 = new Deck();

        int player1Score = 0, player2Score = 0;
        int CardCounter = 0;

        while(CardCounter < 52)
        {
            player1.draw();
            player2.draw();

            System.out.println(player1 + " " + player2);

            CardCounter++;
        }
        System.out.printf("Final score: Player 1--%d; Player 2--%d", player1Score, player2Score);
    }
}

I don't understand why the MainDriver.java:17 is having an error at all. I used abstract in Card because I will also extend it with other class(have not worked on yet) and I will define a different boolean through there. I also have trouble with comparing the cards in the main driver.

I did not include CardValue and CardSuit but they're public enums with suits(Clubs, Diamonds, Hearts, Spades) & values(Two, Three, all the way to Jack, Queen, King, Ace).

user207421
  • 305,947
  • 44
  • 307
  • 483
NoobestPros
  • 65
  • 2
  • 8

2 Answers2

0

It's more likely the error is here :

playingCards[cardNumber].value = cardvalue[j];

since you never assign a new Card to playingCards[cardNumber], which is null.

Change your loop to :

    for (int i = 0; i < 4; i++)
    {
        for(int j = 0; j < 13; j++)
        {
            playingCards[cardNumber] = new Card ();
            playingCards[cardNumber].value = cardvalue[j];
            cardNumber++;
        }
        playingCards[cardNumber].suit = cardsuit[i];
    }

Also change

public abstract class Card

to

public class Card

Since you can't instantiate an abstract class.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • isn't it assigned over here? Card[] playingCards = new Card[52]; – NoobestPros Feb 28 '15 at 03:31
  • Yes ... but that creates an array initialized with 52 `null` references. – Stephen C Feb 28 '15 at 03:33
  • ahh I see, im getting "Cannot instantiate the type Card" from it though – NoobestPros Feb 28 '15 at 03:40
  • @NoobestPros I just noticed your Card class is abstract. That's why you can't instantiate it. Is there a reason for it being abstract? And why would MainDriver extend Card? – Eran Feb 28 '15 at 03:44
  • I was going to create another class that will also extend the Card. the boolean haves different purposes on the main driver and on another class which I have yet to create – NoobestPros Feb 28 '15 at 03:48
0

If you think you are getting an NPE at this line:

  for(int j = 0; j < 13; j++)    //Error here (Deck.java:18)

you are mistaken. An NPE at that line is impossible. Nothing in that particular line uses reference types in any way.

Check that you have recompiled all of your code and that the version of the source code matches the compiled classes that you are using.

If we allow for your mistake with building / running, then @Eran has identified a plausible cause of the NPEs.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216