首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >程序抛出NullPointerException

程序抛出NullPointerException
EN

Stack Overflow用户
提问于 2014-03-12 04:54:13
回答 4查看 156关注 0票数 0

不知道为什么它给了我NullPointerException。请帮帮忙。我非常肯定所有的数组都是满的,并且我限制了所有的循环,以避免传递空空间。

进口java.util.;进口java.io.

公共类TextAnalysis {

代码语言:javascript
复制
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; 
}

}

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2014-03-12 05:21:08

这个代码有两个问题。1.您没有执行空检查,尽管数组包含空值2。您的数组索引从0到8,如果您不想在第9个索引处获得元素,它将抛出ArrayIndexOutOfBound异常。

你的代码应该是这样的

代码语言:javascript
复制
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]++;
    }
    }*/

//不需要像下面这样检查...u可以做的事情

代码语言:javascript
复制
 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]);
}

}

票数 1
EN

Stack Overflow用户

发布于 2014-03-12 05:12:22

您的String String[] words = new String[MAX_WORDS];没有初始化,您只是声明it.All --其内容为null,第31行中的调用length方法将为您提供空指针异常。`

票数 1
EN

Stack Overflow用户

发布于 2014-03-12 04:58:39

简单的错误。当您声明一个数组时,它的大小从0到n-1。此数组仅具有0到8之间的索引。

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22342314

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档