java.util.NoSuchElementException - 在 XSSFSheet 上读取
java.util.NoSuchElementException - on XSSFSheet read
我正在努力解决读取 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);
}
}
}
}
}
当我调用构造函数 Students(location) 时。 eclipse java 总是生成以下错误消息
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)
只要枚举中没有下一个元素,就会抛出 NoSuchElementException,因此您调用的第一个 rowIterator.next() 就是问题所在。我认为这是因为你的 xsl 文档中确实只有一个 sheet,所以你应该得到第一个 sheet,它是 getSheetAt(0) 而不是一个。
我正在努力解决读取 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);
}
}
}
}
}
当我调用构造函数 Students(location) 时。 eclipse java 总是生成以下错误消息
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)
NoSuchElementException,因此您调用的第一个 rowIterator.next() 就是问题所在。我认为这是因为你的 xsl 文档中确实只有一个 sheet,所以你应该得到第一个 sheet,它是 getSheetAt(0) 而不是一个。