Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在此Java代码中防止IndexOutOfBoundsException错误?_Java_Arraylist_Indexoutofboundsexception - Fatal编程技术网

如何在此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
 }