【问题标题】:java.lang.NullPointerException when referencing an array of ints from a different classjava.lang.NullPointerException 引用来自不同类的整数数组时
【发布时间】:2018-07-24 17:35:47
【问题描述】:

我正在做一个项目来遍历一个文本文件并输出文件中每个字母的计数。

import java.util.*;
import java.io.*;
    public class frequencyAnalysis {

        private static String text;
        public static String alphabet;
        public static int Freq[];

    public frequencyAnalysis(String text) {
        this.text = text;
        int [] Freq = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};  //array of ints to keep track of how many of each letter there is.
        alphabet = "abcdefghijklmnopqrstuvwxyz";  //point of reference for the program to know which number in the array should be increased
    }

    public static void freqAnalysis() throws IOException {

        String token = "";
        int index;

        File subset = new File(text);  //creates a new file from the text parameter
        Scanner inFile = new Scanner(subset);

        while(inFile.hasNext()) {
            token = inFile.next();
            index = alphabet.indexOf(token);
            if (index == -1) {  //makes sure that the character is a letter
                break;  
            } else {
                Freq[index]++;
            }
        }
        inFile.close();
    }
 }

这是一个应该遍历给定文本文件的类,并计算其中每个字母的数量。

import java.util.*;
import java.io.*;
public class tester {
    public static void main(String args[]) throws IOException {
        Scanner in = new Scanner(System.in);

        System.out.println("Please type the input file path: ");  //allows the user to specify a file
        String input = in.next();

        frequencyAnalysis Freq = new frequencyAnalysis(input);

        frequencyAnalysis.freqAnalysis();  //calls the method to run through the file

        for(int i = 0; i <= 25; i++){  //prints the alphabet and the Freq array
            System.out.println(frequencyAnalysis.alphabet.charAt(i) + ": " + frequencyAnalysis.Freq[i]);  //this is where the error is
        }

    }
}

这是实现类,它允许用户指定一个文件,然后运行freqAnalysis方法来调整静态Freq数组,然后打印出来。但是,当我运行程序时,它在指定行上给我一个 java.lang.NullPointerException 错误。我已经发现问题出在“frequencyAnalysis.Freq[i]”中,而不是“frequencyAnalysis.alphabet.charAt(i)”中。但是,我不知道问题是什么或如何解决。

【问题讨论】:

标签: java arrays oop static


【解决方案1】:

在构造函数中创建一个初始化局部变量 int [] Freq 而不是类字段。 而不是:

int [] freq = {0, 0, 0, 0, 0, 0, ...

你应该有:

freq = {0, 0, 0, 0, 0, 0, ...

【讨论】:

    【解决方案2】:

    好的,所以您没有在 frequencyAnalysis 类中正确初始化数组。

    public static int Freq[];

    你认为你正在初始化它 int [] Freq = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

    但实际上应该是: this.Freq = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

    【讨论】:

      【解决方案3】:

      其他答案都不错,但在编码风格上有一些缺陷。

      首先,整数数组默认初始化为零,因此您可以简化Freq 的初始化。此外,您在构造函数中为 frequencyAnalysis 创建了一个局部变量 Freq,而不是设置静态类变量

      你应该把你的构造函数改成这样:

          public frequencyAnalysis(String text) {
              this.text = text;
              frequencyAnalysis.Freq = new int[26];  //array of ints to keep track of how many of each letter there is.
              alphabet = "abcdefghijklmnopqrstuvwxyz";  //point of reference for the program to know which number in the array should be increased
          }
      

      此外,类名应始终大写! (改成FrequencyAnalysis

      【讨论】:

      • 我一直将单个单词变量大写,但对于多个单词变量,我不会将第一个单词大写,而是将其余单词大写(例如“userInput”或“thisVariable”。)
      • 我不是在谈论变量,我是说类名应该大写并且在 CamelCase 中。根据Documentation:“类名应该是名词,大小写混合,每个内部单词的第一个字母大写。尽量保持你的类名简单和描述性......”
      【解决方案4】:

      为了让您知道正在发生的事情,

      公共频率分析(字符串文本){ this.text = 文本; int [] 频率 = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; //整数数组以跟踪每个字母有多少。 字母=“abcdefghijklmnopqrstuvwxyz”; //程序的参考点,以知道应该增加数组中的哪个数字 }

      当你这样做时 int [] Freq 是一个新的局部变量,范围仅限于构造函数,而另一方面 public static int Freq[];仍然是Null,指向同一个变量进行初始化,正如其他人所说的那样

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-02
        • 2017-02-23
        • 2020-09-16
        • 2018-08-17
        • 2016-03-02
        • 1970-01-01
        相关资源
        最近更新 更多