gpt4 book ai didi

java - 如何在java中的不同类中创建两个对象引用变量

转载 作者:行者123 更新时间:2023-12-02 06:45:31 26 4
gpt4 key购买 nike

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

最佳答案

你有这个

    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()它初始化索引 1 处的元素, 2 ,和3在你的item数组。

然后循环输入 while并调用

  af.createobject(name, bc, comments);

同一个对象上多次。

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

另一个错误原因(如果解决了上述问题,您就会看到),如果您的 while循环循环超过3次,你会再次得到一堆NullPointerException因为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();'

关于java - 如何在java中的不同类中创建两个对象引用变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18674589/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com