Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Please help! Exception in thread "main" java.lang.NullPointerException

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Please help! Exception in thread "main" java.lang.NullPointerException

    Please could someone check my code and tell me where the problem is? Thanks.

    /** DECK OF CARDS
      * created by Arutha2321
      */
     
      class Card {                				  // A playing card, with variables i - number of card, color - color of card, value - value of card.
        int i;
        String color;
        String value;
     
        public Card (int j, StringBuffer f, StringBuffer h) { // A constructor of a card.
    	this.i = j;
    	this.color = f.toString();
    	this.value = h.toString(); 
        }
     
        public String showCard() {                       	  // A method that returns the color and value of a card in a string.
    	return (this.color + this.value + "\n");
        }
      }
     
      class Deck {						  // A deck of card, with one variable cards, which is an array of instances of class Card.
        Card[] cards;
     
        public Deck() {
          String[] colors = {"heart", "spade", "diamond", "club"};
          String[] values = {"7", "8", "9", "10", "J", "Q", "K", "A"};
     
          StringBuffer co = new StringBuffer();               
          StringBuffer va = new StringBuffer();
     
          for (int i = 0; i < colors.length*values.length; i++) {    // This creates one Card for every combination of color and value.
    	co.append(colors[i%colors.length]);
    	va.append(values[i%values.length]);
    	this.cards[i] = new Card(i,co,va);
          }
        }
     
        public void showDeck() {				 	 //A method that prints out all of the cards in the deck.
          for (int i = 0; i < colors.length*values.length; i++) {
    	System.out.println(this.cards[i].showCard());
          }
        }
      }
     
      public class DeckOfCards {					// A class with the main function, that creates a deck and shows its content.
        public static void main(String[] args) {
          Deck deck1 = new Deck();
          deck1.showDeck();
        }
      }

    Compilation goes all right, but when I want to run the program, I get this error message:

    Exception in thread "main" java.lang.NullPointerException
                   at Deck.<init>(DeckOfCards.java:34)
                   at DeckOfCard.main(DeckOfCards.java:47)
    Last edited by Arutha2321; November 18th, 2009 at 01:26 AM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Please help! Exception in thread "main" java.lang.NullPointerException

    You forgot to initialize the array of cards:

    public Deck()
    {
         Card[] cards = new Card[52];
         // rest of code
    }

  3. #3
    Junior Member
    Join Date
    Nov 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please help! Exception in thread "main" java.lang.NullPointerException

    Thank you very much.

Similar Threads

  1. Replies: 16
    Last Post: August 27th, 2010, 03:30 PM
  2. Replies: 2
    Last Post: March 23rd, 2010, 01:38 AM
  3. Replies: 1
    Last Post: October 25th, 2009, 11:54 AM
  4. Getting "AWT-EventQueue-0" java.lang.NullPointerException error
    By tryingtoJava in forum AWT / Java Swing
    Replies: 9
    Last Post: September 21st, 2009, 10:46 PM
  5. Books and tutorials for beginners in Java and Visual Basic
    By Professor Spider in forum The Cafe
    Replies: 5
    Last Post: April 9th, 2009, 10:57 AM