0

I've tried debugging my program and have found the value of ArrayList<String> fileNames in my private void printFilesIn(String word) to be null. I've read What is a NullPointerException and how do I fix it? and it has been very informative, but I don't understand what I can do to fix the code. It's probably a simple fix in the right place, but I would like to have some help on this.

    import edu.duke.*;
    import java.util.*;
    import java.io.*;

    public class WordsInFiles {
        private HashMap<String,ArrayList<String>> wordInFilesMap;

        public WordsInFiles() {
            wordInFilesMap = new HashMap<String,ArrayList<String>>();
        }
        private void addWordsFromFile(File file) {

            FileResource fileResource = new FileResource(file);
            String fileName = file.getName();
            for (String word : fileResource.words()) {
                if (!wordInFilesMap.containsKey(word)) {
                    ArrayList<String> newList = new ArrayList<String>();
                    newList.add(fileName);
                    wordInFilesMap.put(word, newList);
                }
                else if (wordInFilesMap.containsKey(word) 
                            && !wordInFilesMap.get(word).contains(fileName)) {
                    ArrayList<String> currentList = wordInFilesMap.get(word);
                    currentList.add(fileName);
                    wordInFilesMap.put(word,currentList);
                }
            }
        }
        private void buildWordFileMap() {
            wordInFilesMap.clear();
            DirectoryResource dirResource = new DirectoryResource();
            for (File file : dirResource.selectedFiles()) {
                addWordsFromFile(file);
            }
        }
        private int maxNumber() {
            //returns the maximum number of files any word appears in, considering
            // all words from a group of files.
            int highest = 0;
            for (String word : wordInFilesMap.keySet()) {
                ArrayList<String> currentFileList = wordInFilesMap.get(word);
                int currentNum = currentFileList.size();
                if (currentNum > highest) {
                    highest = currentNum;
                }
            }
            return highest;
        }
        private ArrayList<String> wordsInNumFiles(int number) {
            //returns an ArrayList of words that appear in exactly number files
            ArrayList<String> wordList = new ArrayList<String>();
            for (String word : wordInFilesMap.keySet()) {
                ArrayList<String> currentList = wordInFilesMap.get(word);
                int currentFileCount = currentList.size();
                if (currentFileCount == number) {
                    wordList.add(word);
                }
            }
            return wordList;
        }
        private void printFilesIn(String word) {
            //prints the names of the files this word appears in, one filename per line
            ArrayList<String> fileNames = wordInFilesMap.get(word);
            for (int index=0; index < fileNames.size(); index++) { //HERE!!
                System.out.println(fileNames.get(index));

            }
           }
        public void tester() {
            //call buildWordFileMap to select files and build HashMap of words
            buildWordFileMap();
            //determine maximum number of files any word is in, considering all words
            int fileNum = maxNumber();
            System.out.println("Max number files any word is in: "+fileNum);

            ArrayList<String> wordsInFiles = wordsInNumFiles(fileNum);
            System.out.println("Total words in all files: "+wordsInFiles.size());
            wordsInFiles = wordsInNumFiles(4);
            System.out.println("Total words in four files: "+wordsInFiles.size());
            printFilesIn("laid");//                  
            System.out.println("\n");
            printFilesIn("tree");
            //System.out.println("\nList of words that appear in most files: "+wordsInFiles);
            /**
            *for (int index=0; index < wordsInFiles.size(); index++) {
            *    System.out.println("Files where "+wordsInFiles.get(index)+" appear:");
            *    printFilesIn(wordsInFiles.get(index));
            *}
            *
            *for (String key : wordInFilesMap.keySet()) {
            *    System.out.println("\nWord: "+key+"\tAppears in files: "
            *                        +wordInFilesMap.get(key));
            *}
            */
        }
    }

In the code I've marked down the location of the error by stating HERE!!!

This is the input that I use when testing this program:
cats are funny and cute dogs are silly
love animals cats and dogs love birds and cats These can be found as files at http://www.dukelearntoprogram.com/course3/archives/ProgrammingImprovingGladLibsData.zip

Community
  • 1
  • 1
Bri Jackson
  • 35
  • 1
  • 9
  • What happens if `laid` isn't withing your `wordInFilesMap`? – MadProgrammer Jan 14 '16 at 03:08
  • I've tried another word 'cat' but it still comes out as an error. – Bri Jackson Jan 14 '16 at 03:10
  • 1
    Well, it's `cats` anyway. We're missing both `DirectoryResource` and `FileResource` which will make it impossible for use to test. I suggest adding some `System.out.println` statements in to test the various values and using break points to debug the code, so you can step through it's operations – MadProgrammer Jan 14 '16 at 03:12
  • Yup, that was it. The word had to be in the files. – Bri Jackson Jan 14 '16 at 03:16

0 Answers0