空指针异常Java卡游戏战争

时间:2013-10-18 08:37:40

标签: java nullpointerexception

我正在尝试制作一个模拟纸牌游戏“战争”的程序。

类Project2

package proj2;
import java.util.Scanner;

public class Project2 {

public static void main( String[ ] args)
{
    Scanner keybd = new Scanner( System.in );

    // Get player names
    System.out.println("Welcome to WAR!!");
    System.out.print("Please enter player 1's name: ");
    String p1Name = keybd.nextLine();
    System.out.print("Please enter player 2's name: ");
    String p2Name = keybd.nextLine();

    // Get random number generator (RNG) seed and initialize the game
    System.out.print ("Please enter the RNG seed for shuffling: ");
    long rngSeed = keybd.nextLong();
    Game war = new Game(p1Name, p2Name, rngSeed);
    int turn = 1;

    // While game is being played, print details and results of each turn
    while (!war.gameComplete())
    {
        System.out.printf( "Turn %2d\n", turn);
        System.out.println( "-----");
        System.out.println( war.nextTurn());
        ++turn;
    }

    // All turns complete; print the game results
    System.out.println("Game Over!!");
    System.out.println(war.gameResult());
    }
}

课堂游戏:

package proj2;
public class Game {
private Deck deck;
private Player player1;
private CardPile p1Deck;
private Player player2;
private CardPile p2Deck;
private int warCount;
private int turns;
public Game(String p1, String p2, long rngSeed) 
{
    this.deck = new Deck();
    deck.Shuffle((int) rngSeed);
    this.player1 = new Player(p1, 0, 0);
    this.p1Deck = new CardPile(deck.Deal());
    this.player2 = new Player(p2, 0, 0);
    this.p2Deck = new CardPile(deck.Deal());
    this.turns = 1;
    this.warCount = 0;
}

public String nextTurn()
{ 
    p1Card = p1Deck.drawCard();
(ERROR)---> String p1Turn = player1.getName() + " shows " + p1Card.cardString(); 
    Card p2Card = p2Deck.drawCard();
    String p2Turn = player2.getName() + " shows " + p2Card.cardString();
    String winner = "";
    if(p1Card.getValue() > p2Card.getValue())
    {
        Card[] wonCards = new Card[2];
        wonCards[0] = p1Card;
        wonCards[1] = p2Card;
        p1Deck.addCard(wonCards, 2);
        player1.setCardsWon(wonCards.length);
        winner = player1.getName() + " wins 2 cards" ;
    }
    if(p2Card.getValue() > p1Card.getValue())
    {
        Card[] wonCards = new Card[2];
        wonCards[0] = p2Card;
        wonCards[1] = p1Card;
        p1Deck.addCard(wonCards, 2);
        player2.setCardsWon(wonCards.length);
        winner = "/n" + player2.getName() + " wins 2 cards";
    }
    String warTurn = "";
    if(p1Card.getValue() == p2Card.getValue())
    {
        this.warCount = warCount + 1;
        warTurn = "WAR!!";
        Card[] wonCards = new Card[8]; 
        while(p1Card.getValue() != p2Card.getValue())
        {
            wonCards[0] = p1Deck.drawCard();
            wonCards[2] = p1Deck.drawCard();
            wonCards[4] = p1Deck.drawCard();
            Card p1WarCard = p1Deck.drawCard();
            wonCards[6] = p1WarCard;
            wonCards[1] = p2Deck.drawCard();
            wonCards[3] = p2Deck.drawCard();
            wonCards[5] = p2Deck.drawCard();
            Card p2WarCard = p2Deck.drawCard();
            wonCards[7] = p2WarCard;
            if(p1WarCard.getValue() > p2WarCard.getValue())
            {
                p1Deck.addCard(wonCards, wonCards.length);
                player1.setCardsWon(wonCards.length);
            }
            if(p2WarCard.getValue() > p2WarCard.getValue())
            {
                p2Deck.addCard(wonCards, wonCards.length);
                player2.setCardsWon(wonCards.length);
            }
        }
    }
    String turnResults = p1Turn + "/n" + p2Turn + "/n" + warTurn + winner;
    return turnResults;
}

public boolean gameComplete() 
{
    boolean result = false;
    if(turns == 20) {
        result = true;
    }
    return result;
}

public String gameResult() 
{
    String p1Result = (player1.getName() + " won " + player1.getCardsWon() + "and"
            + player1.getWarsWon() + "war(s)" + "/n");
    String p2Result = (player2.getName() + " won " + player2.getCardsWon() + "and"
            + player1.getWarsWon() + "war(s)" + "/n");
    String winner = "";
    if(player1.getCardsWon() > player2.getCardsWon())
    {
        winner = "Winner: " + player1.getName();
    }
    if(player2.getCardsWon() < player2.getCardsWon())
    {
        winner = "Winner: " + player2.getName();
    }
    if(player1.getCardsWon() == player2.getCardsWon())
    {
        winner = "Game is a draw";
    }
    String finalResult = ("Game Over!!" + "There were " + warCount
            + "war(s)" + p1Result + "/n" + p2Result + "/n" + winner);
    return finalResult;
    }
}

当我尝试访问类Card时,我得到一个空指针异常错误。我不确定是什么导致错误。

public class Card {

    private char rank;
    private String suit;
    private int value;

    public Card(char rank, String suit, int value)
    {
        this.rank = rank;
        this.suit = suit;
        this.value = value;
    }

    public char getRank()
    {
        return rank;
    }     

    public String getSuit()
    {
        return suit;
    }

    public int getValue()
    {
        return value;
    }

    public String cardString()
    {
        String card = (rank + " of " + suit);
         return card; 
    }
}

我尝试使用Google搜索解决方案,它告诉我在引用对象之前初始化对象。我尝试初始化,但它仍然产生空指针异常错误。

我得到的错误就是这个。

Exception in thread "main" java.lang.NullPointerException
    at proj2.Game.nextTurn(Game.java:29)
    at proj2.Project2.main(Project2.java:37)

drawCard()方法来自此类。

package proj2;

public class CardPile {

    private Card[] playerDeck;

    public CardPile(Card[] deck)
    {
        this.playerDeck = deck;
    }

    public Card drawCard()
    {
        Card topCard = playerDeck[0];
        Card[] newPlayerDeck = new Card[playerDeck.length - 1];
        int counter = 0;
        for(int i = 1; i < playerDeck.length; i++)
        {
            newPlayerDeck[counter] = playerDeck[i];
            counter++;
        }
        this.playerDeck = newPlayerDeck;
        return topCard;
    }
}

2 个答案:

答案 0 :(得分:0)

它返回NULL Point Exception,因为您没有传递任何参数。尝试将参数传递给构造函数。

e.g

Card p1Card = new Card(rank,suit,value); 
Card p2Card = new Card(rank,suit,value);

答案 1 :(得分:0)

由于p1Card为null,因此p1Deck.drawCard()返回null - 这意味着playerDeck [0]为null,这意味着新的CardPile(deck.Deal());创建并清空deck,这意味着deck.Deal()中出现了错误。 “有些东西是错的”我的意思是它可能创建并返回一个带有空值的数组,如:[null,null,null,null,null](或者它可能是第一个值是唯一的null)。