title: Caused by: java.util.NoSuchElementException - Using an iterator in Java?
body: <p>I am trying to iterate over a set of information I have in a spreadsheet, my spreadsheet has three columns and 2 rows.</p>
  
  <p>This is my code I am using to iterate over the information,</p>
  
  <pre><code>private List&lt;DataRecord&gt; allData = new ArrayList&lt;&gt;();
  
  public DataRepository(String excelFilePath) {
  
      FileInputStream inputStream;
      try {
          inputStream = new FileInputStream(new File(excelFilePath));
  
          Workbook workbook = new XSSFWorkbook(inputStream);
          Sheet firstSheet = workbook.getSheetAt(0);
          Iterator&lt;Row&gt; iterator = firstSheet.iterator();
  
          while (iterator.hasNext()) {
              DataRecord record = new DataRecord();
  
              Row nextRow = iterator.next();
              Iterator&lt;Cell&gt; cellIterator = nextRow.cellIterator();
              while (cellIterator.hasNext()) {
                  record.setCardNumber(cellIterator.next().getStringCellValue());
                  record.setCvv(cellIterator.next().getStringCellValue());
                  record.setPostcode(cellIterator.next().getStringCellValue());
  
              }
  
              allData.add(record);
          }
  
          inputStream.close();
      } catch (IOException e) {
  
          e.printStackTrace();
      }
  }
  
  public List&lt;DataRecord&gt; getAllData() {
      return allData;
  
  }
  
  public void setAllData(List&lt;DataRecord&gt; allData) {
      this.allData = allData;
  }
  </code></pre>
  
  <p>This worked this morning and now it seems to throw the above exception and I am unsure why .</p>
  
  <p>Thanks</p>
  
  <p>This is the full stack trace </p>
  
  <pre><code>    Caused by: java.util.NoSuchElementException: null
  at java.util.TreeMap$PrivateEntryIterator.nextEntry(TreeMap.java:1205)
  at java.util.TreeMap$ValueIterator.next(TreeMap.java:1252)
  at gov.uk.dwp.epayments.repository.DataRepository.&lt;init&gt;(DataRepository.java:43)
  at gov.uk.dwp.epayments.service.PayService.&lt;init&gt;(PayService.java:11)
  at gov.uk.dwp.epayments.controller.PaymentController.&lt;init&gt;(PaymentController.java:23)
  at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
  at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
  at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
  at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
  at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147)
  ... 18 common frames omitted
  </code></pre>