2015-04-14 20 views
0

我正在努力從讀取xlsx文件中的錯誤。我正在使用poi 3.9-2012。 我的構造函數和其他方法的代碼是:java.util.NoSuchElementException - 在XSSFSheet上讀取

public students(String studentsDb){ 
     this.location = studentsDb; 
     this.location = studentsDb; 
     studentInfoDB = new HashSet<Student>(); 
     DBSetUp(location); 
    } 
private void DBSetUp(String location) { 
     try { 
       this.location = location; 
       FileInputStream file = new FileInputStream(new File(location)); 
       workbook = new XSSFWorkbook(file); 
       setUpStudent(); 
       setUpTeam(); 
     } catch (FileNotFoundException e) { 
      System.out.println("Failed To Find The File!"); 
     } catch (IOException e) { 
      System.out.println("Failed To Create Workbook!"); 
     }   
} 
private void setUpTeam() { 
     XSSFSheet sheet = workbook.getSheetAt(1); 

     Iterator<Row> rowIterator = sheet.iterator(); 
     rowIterator.next(); 
     while (rowIterator.hasNext()) { 

      Row row = rowIterator.next(); 
      Iterator<Cell> cellIterator = row.cellIterator(); 
      Cell cell = cellIterator.next(); 
      String tempTeam = cell.getStringCellValue(); 

      while (cellIterator.hasNext()) { 

       cell = cellIterator.next(); 
       String tempName = cell.getStringCellValue(); 

       for (Student s : studentInfoDB) { 
        if (s.getName().equals(tempName)) { 
         s.setTeam(tempTeam); 
        } 
       } 
      } 
     } 

} 

當我調用構造函數的學生(的位置)。蝕的java總是產生以下錯誤消息

java.util.NoSuchElementException在 java.util.TreeMap中$ PrivateEntryIterator.nextEntry(unknon源)在 java.util.TreeMap中$ ValueIterator.next(未知源)在 Students.setUpTeam(Students.java:77)在 Students.DBSetUp(Students.java:56)在 學生。(Students.java:39)

回答

1

拋出NoSuchElementException每當有沒有下一個元素在枚舉中,所以你調用的第一個rowIterator.next()是問題。我認爲它是因爲你在你的xsl文件中只有一張表,所以你應該得到第一個表格getSheetAt(0)而不是一個表格。

+0

謝謝你的回答。我改變了這一點。這是工作。然而。當我在Excel中添加更多工作表並添加名爲setUpStudent(1)的其他方法時。收到了同樣的錯誤。哪裏不對了? – user3842385

相關問題