user3842385
user3842385

Reputation: 67

java.util.NoSuchElementException - on XSSFSheet read

I am struggling the error from reading xlsx file. I am using poi 3.9-2012. My constructor and other method code are:

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

}

when I call the constructor Students(location). the eclipse java always generate the following error message

java.util.NoSuchElementException at java.util.TreeMap$PrivateEntryIterator.nextEntry(unknon source) at java.util.TreeMap$ValueIterator.next(unknown source) at Students.setUpTeam(Students.java:77) at Students.DBSetUp(Students.java:56) at Students.(Students.java:39)

Upvotes: 0

Views: 2558

Answers (1)

LEADER
LEADER

Reputation: 113

NoSuchElementException is thrown whenever there is no next element in an enumeration, so the first rowIterator.next() you have called is the problem. I think its because you do have just one sheet in your xsl document so, you should get the first sheet which is getSheetAt(0) not one.

Upvotes: 2

Related Questions