为什么这段代码会产生意外的输出?
我有一张桌子。在第一栏中。我有学生的名字,例如“Esfandyar Talebi”和“Arash Nouri”;行数可以更改。 4 行只填充了 2 行。
我写的代码:
List<String> professorsName = new ArrayList<String>();
for(int i = 0; i < InformationTable.getRowCount(); i++) {
professorsName.add((String) InformationTable.getValueAt(i, 0));
System.out.println(professorsName.toString());
}
但这是输出:
[埃斯凡迪亚·塔勒比]
[埃斯凡迪亚·塔勒比、阿拉什·努里]
[Esfandyar Talebi、Arash Nouri、null]
[Esfandyar Talebi、Arash Nouri、null、null]
[Esfandyar Talebi、Arash Nouri、null、null、null]
[Esfandyar Talebi、Arash Nouri、null、null、null、null]
空
I have a table. In the first column. I have students' names, such as "Esfandyar Talebi" and "Arash Nouri"; the number of rows can be changed.
Just 2 rows are filled from 4 rows.
The code that I have written:
List<String> professorsName = new ArrayList<String>();
for(int i = 0; i < InformationTable.getRowCount(); i++) {
professorsName.add((String) InformationTable.getValueAt(i, 0));
System.out.println(professorsName.toString());
}
But this is the output:
[Esfandyar Talebi]
[Esfandyar Talebi, Arash Nouri]
[Esfandyar Talebi, Arash Nouri, null]
[Esfandyar Talebi, Arash Nouri, null, null]
[Esfandyar Talebi, Arash Nouri, null, null, null]
[Esfandyar Talebi, Arash Nouri, null, null, null, null]
null
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
显然,对于
i
的某些值,您的InformationTable.getValueAt(i, 0)
返回null
。你必须查看你的 TableModelApparently your
InformationTable.getValueAt(i, 0)
returnsnull
for certain values ofi
. You'll have to look at your TableModel您发布的循环没有什么奇怪的...它运行的次数与 InformationTable.getRowCount() 方法返回的值一样多,这似乎是六次。因此问题可能出在 InformationTable 类上,如果您需要进一步的帮助,您可能需要将其发布。
There is nothing strange with the loop you posted... It runs as many times as the value InformationTable.getRowCount() method returns, which appears to be six. So the problem is likely on the InformationTable class, you may need to post it if you need further help.
InformationTable 中可能有 6 行。您可能首先必须检查单元格是否已填充名称:
There are probably 6 rows in the InformationTable. You probably first have to check wether the cell has been filled with a name: