为什么这会导致空指针异常?

时间:2014-11-09 05:39:04

标签: java methods arraylist nullpointerexception

我正在尝试用Java编写一个从arraylist手中获取Card对象的方法,并返回手的整数值。每次我尝试运行它时,我都会得到一个空指针异常(请参阅下面的代码中的特定行),但经过一遍又一遍,我似乎无法弄清楚原因。任何人都可以指出我的错误/ s?

private int findHandTotal(){
    Card currentCard;
    int value;
    for (int i=0; i<hand.size(); i++){ //NULL POINTER EXCEPTION HERE
        currentCard=hand.get(i);
        String newCurrentCard= currentCard.toString();

        if(newCurrentCard.contains("Queen")) {
            value =10;

        }
        else if(newCurrentCard.contains("King")) {
            value =10;
        }
        else if(newCurrentCard.contains("Jack")) {
            value =10;
        }
        else if(newCurrentCard.contains("Ace")) {
            value =1;
        }
        else {              
            value = Integer.parseInt(newCurrentCard.substring(0,1));
        }

        handTotal += value;

    }
    return handTotal;
}

我在课程开头做初始化手(见下文)......

public class Player {

private ArrayList<Card> hand; // the player's cards
private int handTotal = findHandTotal(); // The total value of the hand
private Scanner input; 


public Player(){
// player constructor

hand = new ArrayList<Card>();
handTotal=0;
Scanner input = new Scanner(System.in);
}

0 个答案:

没有答案
相关问题