OK. So I've been working on a project and cannot seem to get on why the code is giving me the following error message:
"Exception in thread "main" java.lang.NullPointerException
at Card.getSuit(CardDeck.java:105)
at Card.toString(CardDeck.java:121)
at CardDeck.shuffle(CardDeck.java:33)
at CardDeck.<init>(CardDeck.java:16)
at CardDeck.main(CardDeck.java:70)"
It works when I compile and run it however it throws the exception at the end. It does not give me any errors before I try to run it, it just states that exception. How can I get rid of it? Any help is greatly appreciated.
"Exception in thread "main" java.lang.NullPointerException
at Card.getSuit(CardDeck.java:105)
at Card.toString(CardDeck.java:121)
at CardDeck.shuffle(CardDeck.java:33)
at CardDeck.<init>(CardDeck.java:16)
at CardDeck.main(CardDeck.java:70)"
import java.util.*;
import java.lang.NullPointerException;
import java.util.Random;
public class CardDeck {
private Card[] deck = new Card[52];
public CardDeck() {
for (int i = 0; i < 13; i++) {
deck[i] = new Card(i + 1, "DIAMOND");
deck[i + 13] = new Card(i + 1, "SPADE");
deck[i + 26] = new Card(i + 1, "CLUB");
deck[i + 39] = new Card(i + 1, "HEARTS");
}
shuffle(0); //error
getTopCard();
cutDeck(0);
}
public void shuffle(int randomSeed) {
Card[] temp = new Card[2];
for (int i = 0; i < 20; i++) {
Random b = new Random();
Random a = new Random();
temp[0] = deck[b.nextInt(51)];
deck[b.nextInt(51)] = deck[a.nextInt(51)];
deck[a.nextInt(51)] = temp[0];
}
System.out.print("Shuffled:");
for (int i = 0; i < deck.length; i++) {
System.out.print(deck[i].toString()); //error
System.out.println("\n");
}
}
private Card getTopCard() {
return deck[0];
}
public void cutDeck(int location) {
Random cut = new Random();
int depth = cut.nextInt(51);
Card[] top = new Card[depth];
for (int i = 0; i < depth; i++) {
top[i] = deck[i];
}
// copy all cards after and including the pivot to
// the beginning of the array
for (int i = 0; i < (deck.length - depth); i++) {
deck[i] = deck[depth + i];
}
// copy the cards from the temp array to the end of the array
for (int i = 0; i < depth; i++) {
deck[deck.length - depth + i] = top[i];
}
// return that it worked
System.out.println("Cut with : ");
System.out.println(depth);
for (int i = 0; i < deck.length; i++) {
System.out.print(deck[i].toString());
System.out.println("\n");
}
}
public static void main(String args[]) {
CardDeck launch = new CardDeck(); //error
}
}
class Card {
private static enum Suits {
SPADE, CLUB, DIAMOND, HEART
};
private int value; // default does not make sense!
// public Suits suit;
private Suits suit;
public Card(int aValue, String aSuit) {
value = aValue;
aSuit = aSuit.toUpperCase();
if (aSuit.compareTo("SPADE") == 0)
suit = Suits.SPADE;
if (aSuit.compareTo("CLUB") == 0)
suit = Suits.CLUB;
if (aSuit.compareTo("DIAMOND") == 0)
suit = Suits.DIAMOND;
if (aSuit.compareTo("HEART") == 0)
suit = Suits.HEART;
} // constructor
public int getValue() {
return value;
}
public String getSuit() {
switch (this.suit) { //error
case SPADE:
return "SPADE";
case CLUB:
return "CLUB";
case DIAMOND:
return "DIAMOND";
case HEART:
return "HEART";
}
return null;
}
public String toString() {
return "<" + this.getValue() + " " + this.getSuit() + ">"; //error
}
}
It works when I compile and run it however it throws the exception at the end. It does not give me any errors before I try to run it, it just states that exception. How can I get rid of it? Any help is greatly appreciated.