The first time I tried to use a blog to record my Xiaobai’s learning process, which is mainly convenient for me to query in the future, but it would be great if I could help other friends!
1、 Scenario description
This is the function of reading data from the database and then exporting excel tables. The value is assigned by the iterator to realize the export. The code is as follows:
protected void generate() {
XSSFCellStyle xssfCellStyle = defaultStyle();
defineHeader(this.meta);
headerDataCount = this.defaultSheet.getPhysicalNumberOfRows();
for (int i = 0; i < this.data.size(); i++) {
List<Map<String, Object>> cellList = this.data.get(i);
for (int j = 0; j < cellList.size(); j++) {
renderCell(0, i, String.valueOf(i + 1), xssfCellStyle);
Iterator<Object> iterator = cellList.get(j).values().iterator();
while (iterator.hasNext()) {
String value = String.valueOf(iterator.next());
renderCell(j + 1, i, value, xssfCellStyle);
beautifyColumn(j + 1, value, xssfCellStyle);
}
}
}
}
The problem encountered is that the null value (null value) displayed in the Oracle library is displayed as “null” in the exported excel table. I want to add a judgment during iteration to make the read null directly change to “”.
2、 Something went wrong
I changed it this way:
String value = iterator().next()==null?"":String.valueOf(iterator().next());
However, an error is reported:
java.util.NoSuchElementException
3、 Settle
Cause of problem:
The next () method of iterator class cannot appear twice in the same loop, which will cause the last cursor to point to a null value.
Modification:
Add a variable to receive the iterator:
Object a = iterator.next();
String value = a==null?"":String.valueOf(a);
Read More:
- Error lnk2038: detected “0”_ ITERATOR_ DEBUG_ The reason and solution of the unmatched item of “level”
- data argument can’t be an iterator
- Error lnk2038 occurred during PCL code compilation: detected “error” during PCL code compilation_ ITERATOR_ DEBUG_ Mismatch of level: value ‘0’ does not match value ‘2’ solution
- python reads csv file is an error _csv.Error: iterator should return strings, not bytes (did you open the file in text)
- In Linux shell script, about the commonly used flag [- EQ, GT..] in test and if judgment
- Condition judgment and error handling in ansible playbook (fail module failed_when changed_when keyword)
- Accelerating spark iterative computation with coalesce
- A solution to the problem that the number of nodes does not increase and the name of nodes is unstable after adding nodes dynamically in Hadoop cluster
- Solution to error reporting on the client caused by adding fields on the CXF server
- JQuery is a solution to the disappearance of listening events after adding elements with append
- Solution: when cmake is compiled, “error in configuration process project files may be invalid” appears
- When configuring ROS distributed / Error report solution and command requirements encountered in the process of master-slave computer
- Use jstack to output the stack information in Java process to the specified file and analyze it
- Mybatis error,There is no getter for property named ‘xx’ in ‘class java.lang.String The solution
- Error in adding jshint plug-in to vscode
- Solution to error reporting in Java @ override
- The solution to “polling news feeds” appeared in Eclipse in Java
- Adding project folder in SourceInsight is empty
- Library file error: adding symbols file in wrong format