java.util.NoSuchElementException - 在XSSFSheet上读取

时间:2015-04-14 09:42:34

标签: java apache-poi readfile

我在阅读xlsx文件时遇到了错误。我正在使用poi 3.9-2012。 我的构造函数和其他方法代码是:

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);
                    }
                }
            }
        }

}

当我打电话给构造函数学生(位置)时。 eclipse java总是会生成以下错误消息

  

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

1 个答案:

答案 0 :(得分:2)

如果枚举中没有下一个元素,则抛出NoSuchElementException,因此您调用的第一个rowIterator.next()是问题所在。我认为它是因为你的xsl文件中只有一张纸,所以你应该得到第一张是getSheetAt(0)而不是一张。