Results 1 to 5 of 5
  1. #1
    komo225 is offline Member
    Join Date
    Feb 2009
    Posts
    2
    Rep Power
    0

    Default ArrayList problems

    Hey i'm getting aL

    Exception in thread "main" java.lang.NullPointerException
    at Deck.<init>(Deck.java:24)
    at Poker.main(Poker.java:13)

    when i try to run my client program to my poker game. It builds fine. so i went to my deck file and when i run that, i get:

    java.lang.NoSuchMethodError: main
    Exception in thread "main"

    error when i try to run my Deck file. but it builds fine.

    /**
    * Poker.java

    *
    * @author
    * @version 1.00 2007/10/2
    */

    public class Poker {

    public static void main(String[] args) {

    Deck theDeck = new Deck();
    System.out.println (theDeck);
    theDeck.shuffle();
    System.out.println (theDeck);

    FiveCardHands playerHand = new FiveCardHands(theDeck.deal(5));
    FiveCardHands houseHand = new FiveCardHands (theDeck.deal(5));

    System.out.println ("Player's Hand: " + playerHand);

    System.out.println ("House Hand: " + houseHand);

    System.out.println ("\nThe deck: " + theDeck);
    }
    }

    /**
    * period 3
    * 2/1/09
    *
    *
    * 209195
    */

    import java.util.Random;
    import java.util.ArrayList;

    public class Deck {

    private ArrayList <Card> myCards;
    private ArrayList <Card> shuffled;
    private Random randomGen = new Random();


    public Deck ()
    {
    for(int i = 0; i < 52; i ++)
    {
    Card x = new Card(i);
    myCards.add(i, x); //This is line 24!!
    }

    }

    public void shuffle ()
    {

    for(int i = 0; i < 52; i++)
    {
    int ranNum = randomGen.nextInt(myCards.size());
    shuffled.add(myCards.get(ranNum));
    myCards.remove(ranNum);
    }

    // places cards in random order
    // this is tricky, think before you code!!
    }

    public String toString ()
    {
    String temp = "Hand is [";
    for(int count = 0; count < shuffled.size(); count ++)
    {
    temp = temp + shuffled.get(count);
    if ((shuffled.size() - count) > 1)
    temp = temp + ", ";
    }
    temp =temp + "]";
    return temp;
    }

    public ArrayList <Card> deal (int numCards)
    {
    ArrayList <Card> dealt = new ArrayList<Card>(numCards);
    for(int i = 0; i < numCards; i ++)
    {
    dealt.add(shuffled.get(i));
    shuffled.remove(i);
    }
    return dealt;
    }
    }

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    The deck class can't run by itself (because it doesn't have a main method). The deck class gets instantiated by the Poker class so you can call the methods in the Deck class:
    Java Code:
    Deck theDeck = new Deck();
    theDeck.shuffle();
    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    komo225 is offline Member
    Join Date
    Feb 2009
    Posts
    2
    Rep Power
    0

    Default

    yeah, i understand that, but why won't my poker file run? it says there is a problem with my myCards.add(x); line

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    Is it ?...
    Java Code:
    myCards.add(i, x);
    or
    Java Code:
    myCards.add(x);
    Luck,
    CJSL

    EDIT: now that I've looked at the code better, I think it should be the second one (hint, hint, hint) ... :-)
    Last edited by CJSLMAN; 02-12-2009 at 01:58 AM.
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. [Problems] ArrayList
    By Zuela in forum New To Java
    Replies: 1
    Last Post: 06-16-2008, 12:51 PM
  2. Replies: 2
    Last Post: 04-21-2008, 12:43 PM
  3. ArrayList
    By ramitmehra123 in forum New To Java
    Replies: 1
    Last Post: 02-07-2008, 01:47 AM
  4. ArrayList
    By kizilbas1 in forum New To Java
    Replies: 1
    Last Post: 01-12-2008, 09:48 PM
  5. New to arraylist
    By kleave in forum New To Java
    Replies: 2
    Last Post: 11-19-2007, 07:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •