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, 08: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 06:26 AM.
Reply With Quote Share this thread on Facebook
Sponsored Links
Java Training from DevelopIntelligence
  #2 (permalink)  
Old 18-11-2009, 12:28 AM
helloworld922's Avatar
Super Moderator
 

Join Date: Jun 2009
Posts: 1,329
Thanks: 5
Thanked 282 Times in 254 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

Please surround your code with [highlight=Java]code goes here[/highlight].
Reply With Quote
  #3 (permalink)  
Old 18-11-2009, 06: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 16 27-08-2010 08:30 PM
Getting "AWT-EventQueue-0" java.lang.NullPointerException error tryingtoJava AWT / Java Swing 10 14-05-2010 02:42 PM
Exception in thread "main" java.util.IllegalFormatWidthException: 13 xtrive Exceptions 2 23-03-2010 05:38 AM
Exception in thread "main" java.lang.NoClassDefFoundError: AVTransmit2 NARs Java Networking 1 25-10-2009 03:54 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
2d arraylist java actionlistener actionlistener in java actionlistener java addactionlistener addactionlistener in java addactionlistener java applications of oops could not create java virtual machine xp double format java double to int 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.nullpointerexception exception in thread "main" java.lang.outofmemoryerror: java heap space format double java get mouse position java java 2d arraylist java actionlistener java addactionlistener java convert list to map java double format java double formatting java double to int java double to integer java format double java forum java forums java get mouse position java list to map java mouse position java programming forum java programming forums java programming help java sendkeys java two dimensional arraylist java.lang.classformaterror: truncated class file java.lang.outofmemoryerror: java heap space java.util.arraylist jbutton actionlistener jtextarea font jtextfield font size jxl.read.biff.biffexception: unable to recognize ole stream programming mutators and generics two dimensional arraylist java writing ipod apps

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