Java:我不能为我的生活弄清楚为什么我在这里得到一个NullPointerException

时间:2015-02-20 08:31:09

标签: java list dictionary nullpointerexception set

我正在创建一个基于控制台的Evil Hangman程序。不是在游戏开头选择一个单词,而是通过选择包含用户输入的字母的单词来逐步缩小单词列表。

这是一项非常常见的任务,因此我确信您之前已经听说过类似的内容。

如果您需要更多说明,请参阅以下规范。

Specification

用于编译的必要文件和客户端:

File

HangmanMain

此特定类中有问题的方法是record()方法,这是迄今为止在此分配中编程最困难的方法。 我观察到两个主要问题:

a)有以下NullPointerException,我无法找出原因。它发生在将一个单词添加到列表的行中。据我所知,单词和列表都应该存在并且都被初始化。这不是列表中没有包含单词或列表未正确初始化的问题。它似乎只发生在第二个用户输入之后。

Exception in thread "main" java.lang.NullPointerException
at HangmanManager.record(HangmanManager.java:66)
at HangmanMain.playGame(HangmanMain.java:59)
at HangmanMain.main(HangmanMain.java:39)

b)NullPointerException可能是因为我之前尝试修复另一个问题。在NullPointerException发生之前,我遇到了问题,确定程序选择的单词不符合预期输出的原因。无论我做了什么,程序在遍历结束时选择的单词总是" aa。"即使我故意选择了一个'作为猜测,应该有效地过滤掉了#a;"作为一种选择,它仍然出现了。

到目前为止,这是我的计划。

import java.util.*;
public class HangmanManager {
private String pattern;
private int length;
private int max;
private SortedSet<Character> guessesMade;
private Set<String> words;
private Set<String> currentWords;
private Map<String, Set<String>> patternMap;

public HangmanManager(List<String> dictionary, int length, int max){
    if (length < 1 || max < 0){
        throw new IllegalArgumentException();
    }
    this.length = length;
    this.max = max;
    words = new TreeSet<String>();
    for (String word : dictionary){
        if (word.length() == length){
            words.add(word);
        }
    }
    currentWords = new TreeSet<String>();
    guessesMade = new TreeSet<Character>();
    patternMap = new TreeMap<String, Set<String>>();
    pattern = "";
    for (int i = 0; i < length; i++){
        pattern += "- ";
    }

}
public Set<String> words(){
    return words;
}
public int guessesLeft(){
    return max - guessesMade.size();
}
public SortedSet<Character> guesses(){
    return guessesMade;
}
public String pattern(){
    if (words.isEmpty()){
        throw new IllegalArgumentException("There are no words.");
    }
    return pattern;
}

public int record(char guess){
    if (guessesLeft() < 1 || words.isEmpty()){
        throw new IllegalStateException();  
    }
    if (!words.isEmpty() && guessesMade.contains(guess)) { 
        throw new IllegalArgumentException();
    }
    guessesMade.add(guess);
    int largestOccurences = 0;
    for (String word: words){
        System.out.println(word);
        System.out.println(words.size());
        if (patternMap.containsKey(pattern)){   
            System.out.println("patternMap contains pattern");
            largestOccurences = generatePattern(word, guess);
            currentWords.add(word);
            currentWords = patternMap.get(pattern);
            patternMap.put(pattern, currentWords);          
        } else {
            currentWords.add(word);
            patternMap.put(pattern, currentWords);
        }
    }
    words = findFamily();
    return largestOccurences;
}
private Set<String> findFamily(){
    int maxSize = 0;
    Map <String, Integer> patternCount = new TreeMap<String, Integer>();
    for (String key : patternMap.keySet()){
        patternCount.put(key, patternMap.get(key).size());
            if (patternMap.get(key).size() > maxSize){
                maxSize = patternMap.get(key).size();
                pattern = key;
            } else if (patternMap.get(key).size() == maxSize){
                if (key.length() >= pattern.length()){
                    pattern = key;
                    maxSize = patternMap.get(key).size();
                }
            }
        }
    System.out.println("Current pattern: " + pattern);
    return patternMap.get(pattern);
}

private int generatePattern(String s, char guess) {
    int count = 0;
    pattern = "";
        for (int i = 0; i < length; i++){
            if (s.charAt(i) == guess){
                pattern += guess + " ";
                count++;
            } else {
                pattern += "- ";
            }
        }
    return count;
}
}

2 个答案:

答案 0 :(得分:0)

您的问题出在patternMap

 if (patternMap.containsKey(pattern))
    {   
        System.out.println("patternMap contains pattern");
        largestOccurences = generatePattern(word, guess);
        //=> generatePattern changes the global variable pattern, so it may 
        //not be contained in patternMap anymore

        currentWords.add(word);
        currentWords = patternMap.get(pattern);//since pattern was changed, 
                                               //patternMap return null and  
                                         //you get NullPointerException on 
                                         //the next iteration of the loop

        patternMap.put(pattern, currentWords);          
    }  

当您致电pattern时,您需要确保patternMap中存在新更改的currentWords = patternMap.get(pattern);。否则,您将获得null

答案 1 :(得分:0)

在分配值之前检查null。  currentWords = patternMap.get(pattern);

替换它  如果(patternMap.get(模式)!= NULL)  currentWords = patternMap.get(pattern);

chk this out ...如果你有任何这样的任务,那么空检查......

祝你好运!