Java 程序抛出NullPointerException

Java 程序抛出NullPointerException,java,arrays,nullpointerexception,Java,Arrays,Nullpointerexception,不知道为什么它会给我NullPointerException。请帮忙。 我非常确定所有的数组都已满,并且我限制了所有的循环不能传递空的空间 导入java.util。; 导入java.io 公共类文本分析{ public static void main (String [] args) throws IOException { String fileName = args[0]; File file = new File(fileName); Scanner fileSc

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

导入java.util。; 导入java.io

公共类文本分析{

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; 
}
publicstaticvoidmain(字符串[]args)引发IOException{
字符串文件名=args[0];
文件=新文件(文件名);
Scanner fileScanner=新扫描仪(文件);
int MAX_WORDS=10000;
String[]words=新字符串[MAX_words];
int unique=0;
System.out.println(“文本文件统计”);
System.out.println(“------------------”);
System.out.println(“最长单词的长度:+longestWord(fileScanner));
读取(文字、文件名);
System.out.println(“文件单词列表中的单词数:”+wordlist(单词));
System.out.println(“文件中的字数:”+countWords(文件名)+“\n”);
System.out.println(“词频统计”);
长度频率(字);
System.out.println();
System.out.println(“单词列表转储”);
字频率(字、文件名);
}
公共静态void wordFrequency(字符串[]个单词,字符串文件名)引发IOException{
文件=新文件(文件名);
扫描仪s=新扫描仪(文件);
int[]数组=新的int[words.length];
而(s.hasNext()){
字符串w=s.next();
如果(w!=null){
for(int i=0;i=10){
长度倍[9]++;
}否则{
长度倍[w.length()-1]+;
}
}
}
对于(int j=0;j<10;j++){
System.out.println(“字长”+(j+1)+“:“+长度时间[j]);
}
}
公共静态字符串最长字(扫描器s){
字符串最长=”;
而(s.hasNext()){
字符串word=s.next();
if(word.length()>longest.length()){
最长=单词;
}
}
返回(longest.length()+“”+“(\“”+longest+“\”));
}
公共静态int countWords(字符串文件名)引发IOException{
文件=新文件(文件名);
Scanner fileScanner=新扫描仪(文件);
整数计数=0;
while(fileScanner.hasNext()){
String word=fileScanner.next();
计数++;
}
返回计数;
}
公共静态无效读取(字符串[]个字,字符串文件名)引发IOException{
文件=新文件(文件名);
扫描仪s=新扫描仪(文件);
而(s.hasNext()){
字符串word=s.next();
int i;
对于(i=0;i

}简单的错误。声明数组时,其大小从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
int[]lengthTimes=newint[9];
//这里有一些代码
长度时间[9]+;// 当您声明:

String[] words = new String[MAX_WORDS];
您正在创建一个包含
MAX_WORDS
空值的数组,如果您的输入文件没有将其全部填充,您将在原始文件的第37行得到
NullPointerException

 if(w.length() >= 10) { // if w is null this would throw Npe
要解决此问题,您可以使用列表:

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

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

您的字符串数组
String[]words=新字符串[MAX_words]未初始化,您只是声明它。它的所有内容都为null,调用第31行中的length方法将给您null指针异常。

`此代码有两个问题 1.虽然数组包含空值,但您没有执行空检查 2.您的数组索引为0-8,如果您不想在第9个索引处获取元素,它将抛出ArrayIndexOutofBond异常

您的代码应该是这样的

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]++;
    }
    }*/
公共静态无效长度频率(字符串[]个字){
int[]lengthTimes=新int[9];
for(int i=0;i=10){
长度倍[9]++;
}否则{
长度倍[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]);
}
for(int i=0;i

}

粘贴stacktrace的可能副本。。。请你出示错误页…哪一行是第37行?@BrianRoach这是一个单词数组。它不是空的。我从main传递到方法。虽然这是此代码的另一个问题,但它与
NullPointerException
无关。当我更正int[]lengthTimes=new int[10];它仍然给我同样的错误,同样的错误lines@user2310289但是什么呢?它会抛出一个
ArrayIndexOutOfBoundsException
@BrianRoach,你刚才的评论有什么贡献吗?你想让我删除这个答案,然后等他发帖询问他的索引越界异常吗?这个社区有时非常糟糕…我将lengthTimes数组的大小更改为10,但没有任何更改。。相同的错误看起来您也可以使用映射(java.util.Map)