如何在此Java代码中防止IndexOutOfBoundsException错误?
基本上,我只想在数组列表中没有更多记录时显示一条错误消息。我需要调整什么如何在此Java代码中防止IndexOutOfBoundsException错误?,java,arraylist,indexoutofboundsexception,Java,Arraylist,Indexoutofboundsexception,基本上,我只想在数组列表中没有更多记录时显示一条错误消息。我需要调整什么 public void nextRecord() { if(records.size() > 0) { recordCount++; if(records.get(recordCount) != null) { St
public void nextRecord()
{
if(records.size() > 0)
{
recordCount++;
if(records.get(recordCount) != null)
{
String[] array = records.get(recordCount).split(",");
String item = array[0].trim().replaceAll("\"", "");
String number = array[1].trim();
String cost = array[2].trim();
String amnt = array[3].trim();
txtItem.setText(item);
txtNumber.setText(number);
txtCost.setText(cost);
txtAmount.setText(amnt);
}
}
else if (records.get(recordCount) == null)
{
JOptionPane.showMessa
在调用get()之前检查arrayList的大小
if(recordCount
您不会告诉我们哪行抛出异常,或者“记录”是什么。我们应该猜测吗?(1)您可能需要一个while
循环,而不是if
。(2) recordCount++代码>需要被推到while
循环的末尾。您在读取最后一条记录时会出现错误,不是吗?@devnull是的,您是正确的
if(recordCount< records.size() && records.get(recordCount) != null)
{
//Do the processing
} else {
JOptionPane.showMessa
}