Go Back   Java Programming Forums > Java Standard Edition Programming Help > What's Wrong With My Code?

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 17-11-2009, 09:39 PM
Junior Member
 

Join Date: Nov 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Arutha2321 is on a distinguished road
Default Please help! Exception in thread "main" java.lang.NullPointerException

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

Java Code
/** 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:

Java Code
Exception in thread "main" java.lang.NullPointerException
               at Deck.<init>(DeckOfCards.java:34)
               at DeckOfCard.main(DeckOfCards.java:47)




Last edited by Arutha2321; 18-11-2009 at 07:26 AM.
Reply With Quote Share this thread on Facebook
Sponsored Links
  #2 (permalink)  
Old 18-11-2009, 01:28 AM
helloworld922's Avatar
Super Moderator
 

Join Date: Jun 2009
Posts: 837
Thanks: 5
Thanked 187 Times in 172 Posts
helloworld922 will become famous soon enoughhelloworld922 will become famous soon enoughhelloworld922 will become famous soon enough
Default Re: Please help! Exception in thread "main" java.lang.NullPointerException

You forgot to initialize the array of cards:

Java Code
public Deck()
{
     Card[] cards = new Card[52];
     // rest of code
}
__________________
ASCII a question .. Get an ANSI
Reply With Quote
  #3 (permalink)  
Old 18-11-2009, 07:25 AM
Junior Member
 

Join Date: Nov 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Arutha2321 is on a distinguished road
Default Re: Please help! Exception in thread "main" java.lang.NullPointerException

Thank you very much.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Exception in thread "main" java.lang.NullPointerException oscardog Exceptions 14 11-01-2010 10:36 AM
Exception in thread "main" java.lang.NoClassDefFoundError: AVTransmit2 NARs Java Networking 1 25-10-2009 04:54 PM
Getting "AWT-EventQueue-0" java.lang.NullPointerException error tryingtoJava AWT / Java Swing 9 22-09-2009 03:46 AM
Exception in thread "main" java.util.IllegalFormatWidthException: 13 xtrive Exceptions 1 04-06-2009 03:21 PM
Oh dear, an "I'm a beginner" thread Professor Spider The Cafe 5 09-04-2009 03:57 PM


100 most searched terms
Search Cloud
2 dimensional arraylist java 2d arraylist java actionlistener actionlistener in java actionlistener java actionlistener jbutton addactionlistener addactionlistener java avatar hardware id convert double to integer java double format java double to int java double to integer in java double to integer java eclipse shortcut keys eclipse tutorial for beginners exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.outofmemoryerror: java heap space format double java get mouse position java hardware id avatar java 2 dimensional arraylist java 2d arraylist java actionlistener java addactionlistener java button actionlistener java convert double to int java convert list to map java double format java double to int java double to integer java for beginner eclippse java format double java forum java forums java get mouse position java jbutton java list to map java mouse position java programming forum java programming forums java sendkeys java.lang.reflect.invocationtargetexception java.util.arraylist jbutton actionlistener jbutton java programming forums string to int java two dimensional arraylist java writing apps for ipod touch

All times are GMT. The time now is 02:58 AM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.