Java 爪哇的邪恶刽子手。获取NullPointerException

Java 爪哇的邪恶刽子手。获取NullPointerException,java,nullpointerexception,hashmap,Java,Nullpointerexception,Hashmap,因此,我正在编写一个在控制台中玩的EvilHangman.java游戏。我相信你们都听说过这种类型的实验室,但它基本上尽了最大努力,不让用户赢。每当我输入猜测时,就会出现空指针错误。它与ArrayList有关,以wordsLeft方法为中心。任何和所有的帮助都会很好。我觉得自己很愚蠢,因为过去几天我一直在研究这个问题,所以我没能弄明白。我的任务方法可能不是解决它的最有效方法,但它是我的实验室任务所要求的。有一个word类可以上载字典中的所有英语单词 while循环在我的主类中,它完成了游戏的所有

因此,我正在编写一个在控制台中玩的EvilHangman.java游戏。我相信你们都听说过这种类型的实验室,但它基本上尽了最大努力,不让用户赢。每当我输入猜测时,就会出现空指针错误。它与ArrayList有关,以wordsLeft方法为中心。任何和所有的帮助都会很好。我觉得自己很愚蠢,因为过去几天我一直在研究这个问题,所以我没能弄明白。我的任务方法可能不是解决它的最有效方法,但它是我的实验室任务所要求的。有一个word类可以上载字典中的所有英语单词

while循环在我的主类中,它完成了游戏的所有工作:

while((numGuess != 0) ) {

        if(remaining == true )
            System.out.println("There are " + wordsLeft() + " possible words left.") ;
        System.out.println("You have guessed these letters [" + allLetters + "]. And this is what your word looks like: " + patternHandler() ) ;
        System.out.println("You have " + numGuess + " guess(es) left. Please enter a single letter guess: ") ;
        String temp = keyboard.nextLine() ;
        String guess = temp.toLowerCase() ;
        if(guess.length() > 1) {
            System.out.println("You entered too many characters. Enter a new guess. This did not use one of your guesses.") ;
            temp = keyboard.nextLine() ;
            guess = temp.toLowerCase() ;
        }
        if (alreadyGuessed(guess)){
            System.out.println("You have already guessed that letter, please guess again. This did not use one of your guesses.");
            temp = keyboard.nextLine() ;
            guess = temp.toLowerCase() ;
        }
        if(!alreadyGuessed(guess)) {
            allLetters += guess ; //keep track of all the letters that have already been guessed.
            numGuess--;
            count++ ;
        }
        currentList = Family.biggestList ;
        System.out.println(patternHandler());
    }

    if(solved(patternHandler()))
        System.out.println("You have won the game! Please play again!") ;
    else
        System.out.println("Sorry you have lost.. please try again!") ;

}//main

/**
 * Checks to see if a letter has already been guessed.
 * @param let the user's guess
 * @return True if the letter has been guessed before
 */
private static boolean alreadyGuessed(String let) {
    if(allLetters.length() == 0)
        return false ;
    for(int i = 0; i<=allLetters.length()-1 ; i++) {
        if (allLetters.charAt(i) == let.charAt(0))
            return true;
        }
    return false;
}//containsLetter

/**
 * Decides whether or not the user wants to see how many words are possibly left
 * @param temp user input, either yes or no.
 * @return true if the user inputs yes.
 */
private static boolean runningTotal(String temp) {      
    if (temp.equalsIgnoreCase("yes"))
        return true;
    else
        return false;       
}//runningTotal

/**
 * Checks to see if the user figured out the word.
 * @param wrd the current pattern of the possible words
 * @return true if the word does not contain '-'
 */
private static boolean solved(String wrd){
    for(int i = 0; i<wrd.length(); i++){
        if(wrd.charAt(i) == '-')
            return false ;
    }
    return true ;
}//solved
/**
 * Method to get the current word pattern.
 * @return a string representation of the current word pattern.
 */
private static String patternHandler() {
    String temp = "";
    for(int i = 0; i<length; i++){
        temp+= "-";
    }

    if(count == 0)
        return temp ;
    else
        return Family.maxKey ;
}
/**
 * Allows the user to know how many possible words are left.
 * @param list the current word list of possible end words
 * @return the size of list
 */
private static int wordsLeft() {
    if(count==0)
        return startList.size() ;
    else
        return Family.biggestList.size() ;
}
这部分在我的hashmap类中,它处理我要使用的arraylist:

public static void main(String[] args) {

    int guessesLeft = numGuess ;
    int guessCount = numGuess ;
    initial = Words.sortedWords[length] ; 

    if(guessesLeft == guessCount) {
        getPatternForGuess(guess, initial) ;
        guessCount-- ;
    }
    else
        getPatternForGuess(guess, biggestList) ;

    //the key of the wanted list
    int maxLength = 0; 

    //longest list of patterns
    biggestList = new ArrayList<String>() ;

    for(ArrayList<String> entry : families.values()) {
        if(biggestList == null || entry.size() > biggestList.size())
            biggestList = entry ;
    }
    for(Entry<String, ArrayList<String>> e : families.entrySet()) {
        int len = e.getValue().size() ; //size of the arraylist being looked at
        if(maxKey == null || len > maxLength) {
            maxKey = e.getKey() ;
            maxLength = len ;
        }
    }


}//main

/**
 * This method returns a HashMap of possible patterns and the words they contain.
 * @param guess the user's guess obtained from EvilHangman
 * @param list the list of possible words
 * @return newMap a hashmap of all patterns and the ArrayList of words.
 */
public static HashMap<String, ArrayList<String>> getPatternForGuess(String guess, ArrayList<String> list) {

    HashMap<String, ArrayList<String>> newMap = new HashMap<String, ArrayList<String>>() ;
    String pattern = "" ;


    for(int i = 0; i<list.size(); i++) {
        String word = list.get(i) ;
        pattern = getPatternForWord(guess, word) ;

        if(!newMap.containsKey(pattern)) {
            ArrayList<String> wordList = new ArrayList<String>() ;
            wordList.add(word) ;
            newMap.put(pattern, wordList) ;
        }
        if(newMap.containsKey(pattern)) {
            ArrayList<String> wordList = new ArrayList<String>() ;
            wordList.add(word) ;
        }
    }
    return newMap ;
}//getPatternForGuess


//This method gets the pattern based on your guess and words like 
/**
 * Gets the pattern of the word based on the user guess
 * @param guess the user guess obtained from evil hangman
 * @param word the word pulled from the arraylist being mapped
 * @return word a string representation of the pattern.
 */
public static String getPatternForWord(String guess, String word) {
   char ch = guess.charAt(0) ;
   for(int i = 0; i<word.length(); i++) {
       if(word.charAt(i) != ch)
           word.replace(word.charAt(i), '-') ;
   }
   return word;
}//getPatternForWord

通过使用调试器逐步完成此操作,您学到了什么?始终放置stacktrace,以便人们能够更好地帮助您。是的,很抱歉第一次发布。我只是尽力了。这是我在EvilHangman.WordsLeftevillhangman.java:142在EvilHangman.mainEvilHangman.java:48 142在wordsLeft类中得到的控制台异常,48是调用它的打印行。你说的调试器是什么意思?那么wordsLeft的源代码在哪里?142处的代码行是什么?这是抛出错误的代码行,所以这是您应该查找的地方。因为您提出了问题-调试器是一种特殊的软件,通常内置在您的开发环境中,它允许您执行诸如运行程序的选定部分、在指定条件下在选定位置停止程序等操作,一次一行地遍历它,并在运行时检查变量的值。如果您使用调试器单步执行程序,它会立即向您显示出现空指针异常的原因。我强烈建议您学习使用内置于开发环境中的调试器-这是程序员最有用的技能之一。。。