小编Eli*_*a O的帖子

尝试使用hashmap来计算数组中单词的频率

这是我的代码:

public static void main(String args[]) throws Exception
{
    BufferedReader infile = new BufferedReader(new FileReader(args[0]));
    HashMap<String,Integer> histogram = new HashMap<String,Integer>();
    while ( infile.ready() )
    {   
        String SPACE = " ";
        String [] words = infile.readLine().split(SPACE);

        for (String word : words)
        {
            Integer f = histogram.get(word);
            histogram.put(word,f+1);
        }   
    }
    infile.close();
    printHistogram( histogram );
}
private static void printHistogram( HashMap<String,Integer> hm )
{
    System.out.println(hm);
}
Run Code Online (Sandbox Code Playgroud)

我一直得到"histogram.put(word,f + 1);"的NullPointerException.部分.为什么是这样?

java arrays hashmap nullpointerexception

5
推荐指数
1
解决办法
9469
查看次数

标签 统计

arrays ×1

hashmap ×1

java ×1

nullpointerexception ×1