- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个名为 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/
所以我有这个 UltraTicTacToe 游戏,我正在用 HTML/CSS/JS 编码。它由表中表中的表组成。当您单击 X 或 O 时,我想要突出显示您应该进入的下一个 TicTacToe 牌 ta
Some text Some more text 如何让每个 .example 的 .whatever 包含其 .test 的内容? 例如,如果代码是这样的: Som
我是一名优秀的程序员,十分优秀!