文件名上的java.lang.NullPointerException

时间:2016-01-14 03:06:54

标签: java

我已尝试调试我的程序,并发现ArrayList<String> fileNamesprivate void printFilesIn(String word)的值为null。我已经阅读了What is a NullPointerException and how do I fix it?并且内容非常丰富,但我不知道如何修复代码。它可能是一个在正确位置的简单修复,但我想对此有所帮助。

    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));
            *}
            */
        }
    }

在代码中,我在此处标明了错误的位置!

这是我在测试此程序时使用的输入:
cats are funny and cute dogs are silly
love animals cats and dogs love birds and cats 这些文件可以在http://www.dukelearntoprogram.com/course3/archives/ProgrammingImprovingGladLibsData.zip

的文件中找到

0 个答案:

没有答案