card deck null pointer exception

Page 1 of 1

5 Replies - 243 Views - Last Post: 31 January 2013 - 07:36 AM Rate Topic: -----

#1 sasunatu  Icon User is offline

  • New D.I.C Head

Reputation: -6
  • View blog
  • Posts: 4
  • Joined: 30-January 13

card deck null pointer exception

Posted 30 January 2013 - 11:35 AM

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)"

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.

Is This A Good Question/Topic? 0
  • +

Replies To: card deck null pointer exception

#2 sasunatu  Icon User is offline

  • New D.I.C Head

Reputation: -6
  • View blog
  • Posts: 4
  • Joined: 30-January 13

Re: card deck null pointer exception

Posted 30 January 2013 - 12:01 PM

View Postsasunatu, on 30 January 2013 - 11:35 AM, said:

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)"

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.


OK so now it does not compile, just throws the exception. BTW when it was compiling, it did not print out all the cards in the deck.

OK so now it does not compile, just throws the exception. BTW when it was compiling, it did not print out all the cards in the deck.
Was This Post Helpful? 0
  • +
  • -

#3 sasunatu  Icon User is offline

  • New D.I.C Head

Reputation: -6
  • View blog
  • Posts: 4
  • Joined: 30-January 13

Re: card deck null pointer exception

Posted 30 January 2013 - 12:12 PM

View Postsasunatu, on 30 January 2013 - 12:01 PM, said:

View Postsasunatu, on 30 January 2013 - 11:35 AM, said:

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)"

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.


OK so now it does not compile, just throws the exception. BTW when it was compiling, it did not print out all the cards in the deck.

OK so now it does not compile, just throws the exception. BTW when it was compiling, it did not print out all the cards in the deck.

Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2204
  • View blog
  • Posts: 5,236
  • Joined: 10-September 10

Re: card deck null pointer exception

Posted 30 January 2013 - 03:40 PM

I assume that quoting yourself multiple times is your way of bumping your thread. If you've changed your code, post the updated code. There's little point in helping you with old code when you're working on a newer version with new problems.
Was This Post Helpful? 0
  • +
  • -

#5 sasunatu  Icon User is offline

  • New D.I.C Head

Reputation: -6
  • View blog
  • Posts: 4
  • Joined: 30-January 13

Re: card deck null pointer exception

Posted 30 January 2013 - 05:48 PM

View PostGregBrannon, on 30 January 2013 - 03:40 PM, said:

I assume that quoting yourself multiple times is your way of bumping your thread. If you've changed your code, post the updated code. There's little point in helping you with old code when you're working on a newer version with new problems.


View Postsasunatu, on 30 January 2013 - 05:45 PM, said:

View PostGregBrannon, on 30 January 2013 - 03:40 PM, said:

I assume that quoting yourself multiple times is your way of bumping your thread. If you've changed your code, post the updated code. There's little point in helping you with old code when you're working on a newer version with new problems.

The new problem I had had nothing to do with the old one. Anyway I fixed all my problens and its running great..next time u offer shiity advice why dont u try sticking it back up ur ass.
Was This Post Helpful? -6
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10668
  • View blog
  • Posts: 39,618
  • Joined: 27-December 08

Re: card deck null pointer exception

Posted 31 January 2013 - 07:36 AM

Personal attacks are not okay. I'm closing this thread and banning the OP.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1