En lille bug?
Hej.Jeg er ved at skrive en poker program. Jeg har en enkelt bug der har plaget mig noget. Her følger kildekoden til det mest relevante:
/*
* Player.java
*/
package pokertexasholdem;
import java.util.*;
import java.lang.Integer;
/**
* Player class
* Manage the the players name, cards and money
*/
public class Player {
private String name;
private int money;
private ArrayList<Card> handCards;
private ArrayList<Card> availableCards;
/** Creates a new instance of Player
* @param name
* @param money
*/
/** Creates a new instance of Player */
public Player(String name, int money) {
name = name;
money = money;
// handCards manage a list of the 2 cards on the players hand
handCards = new ArrayList<Card>(2);
availableCards = new ArrayList<Card>(7);
}
/** Add a card to the players hand
* @param cardToAdd the card object to add
*/
public void addCard(Card cardToAdd) {
handCards.add(cardToAdd);
}
/** Get a list of the players available cards (hand+community)
* @return a array with the players available cards
*/
public ArrayList<Card> availableCards() {
// combines the handCards array (from the player class) and the CommunityCards array (from the dealer class).
availableCards.addAll(Dealer.getCommunityCards()); // HER ER DER PROBLEMER!!
availableCards.addAll(handCards);
return(availableCards);
}
}
/*
* Dealer.java
*/
package pokertexasholdem;
import java.util.ArrayList;
public class Dealer {
private static CommunityCards cCards;
/** Creates a new instance of Dealer */
public Dealer() {
CommunityCards cCards = new CommunityCards();
Card card1 = new Card("hearts", 1);
Card card2 = new Card("hearts", 2);
Card card3 = new Card("hearts", 10);
Card card4 = new Card("hearts", 11);
Card card5 = new Card("hearts", 12);
Card card6 = new Card("hearts", 13);
Card card7 = new Card("hearts", 14);
cCards.addCard(card1);
cCards.addCard(card2);
cCards.addCard(card3);
cCards.addCard(card4);
cCards.addCard(card5);
dialog.Print("Comm cards: "+cCards.getList().toString());
// opret kort hos player
Player player1 = new Player("Peter", 100);
player1.addCard(card6);
player1.addCard(card7);
ArrayList p1 = player1.availableCards();
dialog.Print("alle korts mulige for player 1: "+p1.toString());
}
/** Get a list of the community cards
* @return an array with the community cards
*/
public static ArrayList<Card> getCommunityCards() {
return cCards.getList();
}
}
/*
* CommunityCards.java
*/
package pokertexasholdem;
import java.util.ArrayList;
/**
* CommunityCards class
* Manage the community cards
*/
public class CommunityCards {
private ArrayList<Card> cardList;
/** Creates a new instance of CommunityCards */
public CommunityCards() {
// cardList manage a list of the 5 community cards
cardList = new ArrayList<Card>(5);
}
/** Add a card to the community cards
* @param cardToAdd the card object to add
*/
public void addCard(Card cardToAdd) {
cardList.add(cardToAdd);
}
/** Get a list of community cards
* @return a array list with the community cards
*/
public ArrayList<Card> getList() {
return(cardList);
}
}
Det var lige de indvoldverede klasser skåret ned til det nødvendige. Card klassen er blot en kort klasse.
Fejlen kommer i player klassen i linjen:
availableCards.addAll(Dealer.getCommunityCards()); // HER ER DER PROBLEMER!!
Det er som om referencen Dealer.getComminityCards ikke virker. Udkommenter man denne linje er der ingen problemer. Kompileren er ikke til meget hjælp:
run:
Welcome to Texas Hold'em
Comm cards: [pokertexasholdem.Card@1820dda, pokertexasholdem.Card@15b7986, pokertexasholdem.Card@87816d, pokertexasholdem.Card@422ede, pokertexasholdem.Card@112f614]
Exception in thread "main" java.lang.NullPointerException
at pokertexasholdem.Dealer.getCommunityCards(Dealer.java:51)
at pokertexasholdem.Player.availableCards(Player.java:49)
at pokertexasholdem.Dealer.<init>(Dealer.java:42)
at pokertexasholdem.TexasHoldemPoker.main(TexasHoldemPoker.java:27)
Java Result: 1
Som det kan ses er den ikke til nogen hjælp. Fejlen ligger i den der reference, som nævnt.
Er der nogen der har et bud?