I am getting null pointer exception while generating .xls file using apache poi in java.
The same code is working fine with "English Language" but its gives me null pointer while trying to generate in "French" Language.
Some of the column having null value but I am handling those null data and blank cell while creating cell
getting null pointer while trying to write the out details
"classExcel.write(out)";
Below is the method I am using for xls generation after all data I have kept in the DTO.
public void ExcelGenerate(
ClassExcelDTO classExcel,
HttpServletResponse response,
HttpServletRequest request) throws Exception {
request.setCharacterEncoding("iso-8859-1");
if( classExcel != null ){
response.reset();
response.setContentType("application/csv");
response.setHeader("Content-disposition","attachment;filename=\"" + classExcel.getName + ".xls\"");
try {
if(null!=response && null!=response.getOutputStream()){
OutputStream out = response.getOutputStream();
if(null!=out)
try {
classExcel.write(out);
} catch (Exception e) {
e.printStackTrace();
}
out.close();
}
} catch (SocketException sEx) {
} catch (IOException io) {
} catch (Exception e) {
}
} else {
// Some code
}
}
Below is the generated exception : 14:43:09,037 ERROR [stderr] (http-localhost/127.0.0.1:8080-4) java.lang.NullPointerException
14:43:09,037 ERROR [stderr] (http-localhost/127.0.0.1:8080-4) at org.apache.poi.util.StringUtil.putCompressedUnicode(StringUtil.java:193)
14:43:09,037 ERROR [stderr] (http-localhost/127.0.0.1:8080-4) at org.apache.poi.hssf.record.BoundSheetRecord.serialize(BoundSheetRecord.java:281)
14:43:09,037 ERROR [stderr] (http-localhost/127.0.0.1:8080-4) at org.apache.poi.hssf.model.Workbook.serialize(Workbook.java:732)
14:43:09,037 ERROR [stderr] (http-localhost/127.0.0.1:8080-4) at org.apache.poi.hssf.usermodel.HSSFWorkbook.getBytes(HSSFWorkbook.java:786)
14:43:09,037 ERROR [stderr] (http-localhost/127.0.0.1:8080-4) at org.apache.poi.hssf.usermodel.HSSFWorkbook.write(HSSFWorkbook.java:732)
Please help me if any one having any idea.
I am able to solve above null pointer exception.
Root Cause of the issue.
While creating each tab using French language I was passing keyValue of a tab name which contains more than 31 Character. but Apache POI is allowed less than 31 character (Exception--------------java.lang.IllegalArgumentException: Sheet name cannot be blank, greater than 31 chars, or contain any of /*?[] ) and due to this blank(null) sheet name this null pointer exception occurred while writing the workbook.
so avoid the sheet name having less than 31 charter and sheet name should not contain any of /*?[] special character.