0

I'm having trouble with this code. I copied this code from the book.. What is the problem??

import java.io.*;
import java.util.*;

public class WordCount {
    // minimum number of occurrences needed to be printed

    public static final int OCCURRENCES = 2000;

    public static void main(String[] args) throws FileNotFoundException {
        System.out.println("This program displays the most");
        System.out.println("frequently occurring words from");
        System.out.println("the book Moby Dick.");
        System.out.println();

        // read the book into a map
        Scanner in = new Scanner(new File("moby.txt"));
        Map<String, Integer> wordCountMap = getCountMap(in);

        for (String word : wordCountMap.keySet()) {
            int count = wordCountMap.get(word);
            if (count > OCCURRENCES) {
                System.out.println(word + " occurs " + count + " times.");
            }
        }
    }

    // Reads book text and returns a map from words to counts.
    public static Map<String, Integer> getCountMap(Scanner in) {
        Map<String, Integer> wordCountMap = new TreeMap<String, Integer>();

        while (in.hasNext()) {
            String word = in.next().toLowerCase();
            if (wordCountMap.containsKey(word)) {
                // never seen this word before
                wordCountMap.put(word, 1);
            } else {
                // seen this word before; increment count
                int count = wordCountMap.get(word);
                wordCountMap.put(word, count + 1);
            }
        }

        return wordCountMap;
    }
}
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
GanjaMan
  • 23
  • 1
  • 1
  • 5
  • you can use `.Integer`. instead of `.int`. to avoid automatic unboxing (conversion) and `.NullPointerException`. – Kaumadie Kariyawasam Apr 02 '21 at 14:09
  • 1
    `if (wordCountMap.containsKey(word)) { // never seen this word before` this is the exact opposite of what the comment says. You probably forgot to copy a `!` and this is actually `if (!wordCountMap.containsKey(word))` – Federico klez Culloca Apr 02 '21 at 14:11
  • @FedericoklezCulloca yes brother here was a bug. Thank you so much)) I copied it from book, like ctrl + c , ctrl + v.. So it's a typo I think in a book. Btw thanks – GanjaMan Apr 02 '21 at 14:48

1 Answers1

0

Map is declared as Map<String, Integer> which means values are objects of type Integer and they can be null. On the other hand, in line int count = wordCountMap.get(word); variable count is of type int which cannot be null. And in this situation Java uses autoboxing to convert Integer to int calling Integer.intValue() under the hood, but what happens if returned value is null? Calling any method on null will throw NullPointerException like you have here.

To summarize: java doesn't know how to convert null of Integer type into int type.

Flame239
  • 1,274
  • 1
  • 8
  • 20
  • Ok, but the problem here was *why* that call to `get` returned null. As per my comment, there was a typo in the `if` condition. *That* is the problem with this code. – Federico klez Culloca Apr 02 '21 at 14:52