Results 1 to 5 of 5
Thread: ArrayList problems
- 02-11-2009, 11:11 PM #1
Member
- Join Date
- Feb 2009
- Posts
- 2
- Rep Power
- 0
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;
}
}
- 02-11-2009, 11:28 PM #2
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:
Luck,Java Code:Deck theDeck = new Deck(); theDeck.shuffle();
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-11-2009, 11:39 PM #3
Member
- Join Date
- Feb 2009
- Posts
- 2
- Rep Power
- 0
yeah, i understand that, but why won't my poker file run? it says there is a problem with my myCards.add(x); line
- 02-12-2009, 12:23 AM #4
Is it ?...
orJava Code:myCards.add(i, x);
Luck,Java Code:myCards.add(x);
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 12:58 AM.
Chris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-12-2009, 04:14 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Can you show your class name Card. As CJSL mentioned in the above post, something wrong with the arguments passing.
Similar Threads
-
[Problems] ArrayList
By Zuela in forum New To JavaReplies: 1Last Post: 06-16-2008, 11:51 AM -
Java Project Trouble: Searching one ArrayList with another ArrayList
By BC2210 in forum New To JavaReplies: 2Last Post: 04-21-2008, 11:43 AM -
ArrayList
By ramitmehra123 in forum New To JavaReplies: 1Last Post: 02-07-2008, 12:47 AM -
ArrayList
By kizilbas1 in forum New To JavaReplies: 1Last Post: 01-12-2008, 08:48 PM -
New to arraylist
By kleave in forum New To JavaReplies: 2Last Post: 11-19-2007, 06:45 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks