该程序抛出NullPointerException

时间:2014-03-12 04:54:13

标签: java arrays nullpointerexception

不确定为什么它会给我NullPointerException。请帮忙。 我很确定所有数组都已满,并且我限制所有循环不要通过空格。

import java.util。; import java.io。;

public class TextAnalysis {

public static void main (String [] args) throws IOException {
    String fileName = args[0];
    File file = new File(fileName);
    Scanner fileScanner = new Scanner(file);
    int MAX_WORDS = 10000;
    String[] words = new String[MAX_WORDS];
    int unique = 0;

    System.out.println("TEXT FILE STATISTICS");
    System.out.println("--------------------");
    System.out.println("Length of the longest word: " + longestWord(fileScanner));
    read(words, fileName);
    System.out.println("Number of words in file wordlist: " + wordList(words));
    System.out.println("Number of words in file: " + countWords(fileName) + "\n");
    System.out.println("Word-frequency statistics");
    lengthFrequency(words);
    System.out.println();
    System.out.println("Wordlist dump:");
    wordFrequency(words,fileName);
}

public static void wordFrequency(String[] words, String fileName) throws IOException{
    File file = new File(fileName);
    Scanner s = new Scanner(file);
    int [] array = new int [words.length];
    while(s.hasNext()) {
        String w = s.next();
        if(w!=null){
            for(int i = 0; i < words.length; i++){
                if(w.equals(words[i])){
                    array[i]++;
                }
            }
            for(int i = 0; i < words.length; i++){
            System.out.println(words[i] + ":" + array[i]);
            }
        }
    }
}

public static void lengthFrequency (String [] words) {
    int [] lengthTimes = new int[10];

    for(int i = 0; i < words.length; i++) {
        String w = words[i];
        if(w!=null){
            if(w.length() >= 10) {
            lengthTimes[9]++;
            } else {    
            lengthTimes[w.length()-1]++;
            }
        }
    }
    for(int j = 0; j < 10; j++) {
        System.out.println("Word-length " + (j+1) + ": " + lengthTimes[j]);
    }
}

public static String longestWord (Scanner s) {
    String longest = "";
    while (s.hasNext()) {
        String word = s.next();
        if (word.length() > longest.length()) {
            longest = word;
        }
    }
    return (longest.length() + " " + "(\"" + longest + "\")");
}

public static int countWords (String fileName) throws IOException {
    File file = new File(fileName);
    Scanner fileScanner = new Scanner(file); 
    int count = 0;

        while(fileScanner.hasNext()) {
            String word = fileScanner.next();
                count++;
        }
    return count;
}


public static void read(String[] words, String fileName) throws IOException{
    File file = new File(fileName);
    Scanner s = new Scanner(file);
    while (s.hasNext()) {
    String word = s.next();
    int i;
    for ( i=0; i < words.length && words[i] != null; i++ ) {
        words[i]=words[i].toLowerCase();
        if (words[i].equals(word)) {
            break;
        }
    }
    words[i] = word;
}
}

public static int wordList(String[] words) {
    int count = 0;
    while (words[count] != null) {
        count++;
    }
 return count; 
}

}

4 个答案:

答案 0 :(得分:1)

您的字符串数组String[] words = new String[MAX_WORDS];未初始化,您只是声明它。它的所有内容都为null,第31行中的调用长度方法将为您提供空指针异常。 `

答案 1 :(得分:1)

此代码存在两个问题 虽然数组包含空值,但您没有进行空检查 2.你的数组索引从0到8,如果你不想获得第9个索引的元素,它将抛出ArrayIndexOutOfBound Exception。

您的代码应该是那样的

public static void lengthFrequency (String [] words) {
int [] lengthTimes = new int [9];

for(int i = 0; i < words.length; i++) {
    String w = words[i];
    if(null!=w) //This one added for null check
    {
  /*  if(w.length() >= 10) {
    lengthTimes[9]++;
    } else {    
    lengthTimes[w.length()-1]++;
    }
    }*/

//不要那样检查......你可以这样做

 for(int i = 0; i < words.length; i++) {
    String w = words[i];
    if(null!=w)
    {
        lengthTimes[i] =w.length();
    }
}
}


//here we should traverse upto length of the array.
for(int i = 0; i < lengthTimes.length; i++) { 
    System.out.println("Word-length " + (i+1) + ": " + lengthTimes[i]);
}

}

答案 2 :(得分:0)

简单的错误。声明数组时,它的大小为0到n-1。此数组仅包含0到8之间的索引。

int [] lengthTimes = new int [9];
//some code here
lengthTimes[9]++; // <- this is an error (this is line 29)

for(int i = 0; i < 10; i++) {
    System.out.println("Word-length " + (i+1) + ": " + lengthTimes[i]); // <- same error when i is 9. This is line 37

答案 3 :(得分:0)

宣布:

String[] words = new String[MAX_WORDS];

你正在创建一个包含MAX_WORDS空值的数组,如果你的输入文件没有全部填充,你会得到一个NullPointerException我认为你原来的文件中的第37行:

 if(w.length() >= 10) { // if w is null this would throw Npe

要修复它,您可以改用List:

 List<String> words = new ArrayList<String>();
 ...
 words.add( aWord );

或者,如果您不想重复单词,也许可以使用Set。