0

我有一个名为 AgendaFunctions 的类、一个名为 Main 的类和一个名为 ReadFiles 的类。Main 有一个用于议程函数和 ReadFiles 的参考变量。AgendaFunctions 有一个引用变量数组。我有实例化数组的代码,但我需要从 ReadFiles 实例化它。如果我从 main 实例化它,它工作正常。但是如果我从 ReadFiles 调用该方法,它就不起作用。我收到 java.lang.NullPointerException 错误。这是主要的代码:

    public class Main {
        public static void main(String[] args) throws Exception {
            ReadFiles fw = new ReadFiles(); fw.read();
            agendafunctions link = new agendafunctions();

议程职能:

    public class agendafunctions {
        int amount = 20;
        public void setamount(int data) {

        }
        static String input = "true";
        agendaitem item[] = new agendaitem[amount];
        int counter = 0;
        public void instantiate() {
                item[1] = new agendaitem();
                item[2] = new agendaitem();
                item[3] = new agendaitem();
        }
        public void createobject(String name, Boolean complete, String Comments) {
            item[counter].name = name;
            item[counter].complete = complete;
            item[counter].comments = Comments;
            counter++;
        }

读取文件:

    public class ReadFiles {
        public void read() throws IOException {
            agendafunctions af = new agendafunctions(); af.instantiate();
            int readitem = 1;
            BufferedReader data = new BufferedReader(new FileReader("C:/Agenda Dev Docs/data.txt"));
            int filestoread = Integer.parseInt(data.readLine());
            while (readitem <= filestoread) {
                String name;
                String complete;
                String comments = null;
                String line;
                Boolean bc = null;
                BufferedReader read = new BufferedReader(new FileReader("C:/Agenda Dev Docs/"+readitem+".txt"));
                readitem++;
                name = read.readLine();
                complete = read.readLine();
                comments = "";
                while((line = read.readLine()) != null) {
                    comments = comments + line;
                }
                if(complete.equals("Complete")) {
                    bc = true;
                } else if(complete.equals("Incomplete")) {
                    bc = false;
                }
                af.createobject(name, bc, comments);
            }
        }

如果我从 ReadFiles 调用方法实例化,我会得到 NullPointerException。如果我从 Main 调用它,一切正常。但进一步的开发需要我从 ReadFiles 中调用该方法。我将如何解决这个问题?谢谢。

4

2 回答 2

2

你有这个

    int counter = 0;
    public void instantiate() {
            item[1] = new agendaitem();
            item[2] = new agendaitem();
            item[3] = new agendaitem();
    }
    public void createobject(String name, Boolean complete, String Comments) {
        item[counter].name = name;
        item[counter].complete = complete;
        item[counter].comments = Comments;
        counter++;
    }

其中item是具有 20 个索引的数组,即 20 个元素,但您的instantiate方法仅初始化索引 1 到 3 处的元素,缺少 0 和 4 到 19。

在你的ReadFiles#read()方法中,你做

  agendafunctions af = new agendafunctions(); af.instantiate();

它实例化一个 agendafunctions对象并调用它在索引、和数组中instantiate()初始化元素。123item

然后你循环while并调用

  af.createobject(name, bc, comments);

在同一个物体上多次。

第一次失败的原因是你没有item在索引 0 处初始化元素。数组总是从 开始0,而不是 1。

另一个错误原因(如果你解决了上述问题,你会看到),如果你的while循环循环超过 3 次,你会再次得到一堆NullPointerExceptions 因为counter不断增长,但你没有初始化元素然后您将尝试在counter索引处访问。

item[counter].name = name; // if counter is 4, you'll get NullPointerException because 
                           // the element there hasn't been initialized as 'new agendaitem();'
于 2013-09-07T15:13:45.767 回答
0

@SotiriosDelimanolis 解释了你为什么NPE要解决这个问题,你可以摆脱该instantiate()方法并添加item[counter] = new agendaitem();为方法的第一行createobject。此外,您必须确保您的 while 循环不超过amount. 为了避免这些担忧,最好使用ArrayListagendaitem

于 2013-09-07T15:21:08.950 回答