0

I want to create an ArrayList of type Card

My Card class will have 2 instance variables of type Suit called suit and of type String called rank. The type Suit is its own subclass with 2 instance variables of type String called suitTitle and type Boolean called selected.

My card Class looks like this

public class Card {
public Suit suit;
public String rank;

With the constructor and all the obvious getters and setters

My Suit Class looks like this

public class Suit {
public String suitTitle;
public Boolean selected;

With the constructor and all the obvious getters and setters

When creating the cards First I need an Array of Suit type that will look like this

Suit[] suit = new Suit[4];
suit[0].suitTitle = "Spades";
suit[1].suitTitle = "Hearts";
suit[2].suitTitle = "Clubs";
suit[3].suitTitle = "Diamonds";

But when trying to put in the values, I get a nullpointer exception

Joshua
  • 19
  • 1
  • 3

3 Answers3

3

When you do this:

Suit[] suit = new Suit[4];

... you are creating an array that is large enough to hold 4 Suit objects, but each object in the array is null.

Therefore you need to do this:

Suit[] suit = new Suit[4];

// put actual Suit objects in the array
suit[0] = new Suit();
suit[1] = new Suit();
suit[2] = new Suit();
suit[3] = new Suit();

suit[0].suitTitle = "Spades";
suit[1].suitTitle = "Hearts";
suit[2].suitTitle = "Clubs";
suit[3].suitTitle = "Diamonds";
Jason
  • 11,744
  • 3
  • 42
  • 46
-1

You are initializing array of suit but for each location of array suit object is null so you are getting NullPointerException. Please Check this:

public class Card {
    public Suit[] suit;
    public String rank;
}

public class Suit {
    public String suitTitle;
    public Boolean selected;
}

public class TestCard {
    public static void main(String[] args) {
       Suit[] suitList = new Suit[4];
       Suit suit1 = new Suit();
       suit1.suitTitle = "Spades";
       suitList[0] = suit1;
       Suit suit2 = new Suit();
       suit2.suitTitle = "Hearts";
       suitList[1] = suit2;
       Suit suit3 = new Suit();
       suit3.suitTitle = "Clubs";
       suitList[2] = suit3;
       Suit suit4 = new Suit();
       suit4.suitTitle = "Diamonds";
       suitList[3] = suit4;
       Card card = new Card();
       card.rank = "xyz";
       card.suit = suitList;
   }
}
NullPointer
  • 7,094
  • 5
  • 27
  • 41
  • Code-only answers are discouraged. Instead, add a description of the problem and an explanation as to how your code fixes the problem. --- "*`String args[]`*" - In Java, it is convention to write the array-brackets after the type, not after the name: `String[] args`. – Turing85 Jun 22 '18 at 00:30
  • Thanks.I updated. – NullPointer Jun 25 '18 at 09:44
-2

Instead of having a suit class, I would recommend you store the suit as a number 1-4 in the card class and have a getSuit() function which returns a String. You will be easily able to compare suits if you order them in such a way where the more valuable suit is a larger number as they will already be integers.

String getSuit(){
    if(suit == 1){
        return "the suit";
    }
    ...
}
Lucas B
  • 17
  • 2
  • 1
    "*I would recommend you store the suit as a number 1-4*" - No. This is what [`Enum`s](https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html) are for. – Turing85 Jun 22 '18 at 00:26
  • @Turing85 In many card games, players wish to compare cards and if they have the same value then many times the suit is compared. If stored as an integer, it would allow for easy comparison. – Lucas B Jun 22 '18 at 00:30
  • [`Enum#compareTo(...)`](https://docs.oracle.com/javase/7/docs/api/java/lang/Enum.html#compareTo(E)), [`Enum#ordinal()`](https://docs.oracle.com/javase/7/docs/api/java/lang/Enum.html#ordinal()) – Turing85 Jun 22 '18 at 00:33
  • @Turing85 Never knew that existed. Seems like a good solution. – Lucas B Jun 22 '18 at 00:34