我有一个名为 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 中调用该方法。我将如何解决这个问题?谢谢。