是我的方法的一部分,它在获取CellFormat实例时导致下面给出的异常。请提供任何解决方案来提取excel文件中显示的数据。
try
{
String dataStr = null;
style = cell.getCellStyle();
dataStr = style.getDataFormatString();
CellFormat cf = CellFormat.getInstance(dataStr);
if(cf != null)
{
CellFormatResult result = cf.apply(cell); // -> this is the line which exactly giving exception.
if(!result.text.equals(EMPTY))
content = result.text;
}
}
catch(Exception e)
{
System.out.println("row " + i + " column " + j);
e.printStackTrace();
}
java.lang.NullPointerException
at org.apache.poi.ss.format.CellElapsedFormatter.<init>(CellElapsedFormatter.java:143)
at org.apache.poi.ss.format.CellFormatType$4.formatter(CellFormatType.java:60)
at org.apache.poi.ss.format.CellFormatPart.getFormatter(CellFormatPart.java:280)
at org.apache.poi.ss.format.CellFormatPart.<init>(CellFormatPart.java:172)
at org.apache.poi.ss.format.CellFormat.<init>(CellFormat.java:157)
at org.apache.poi.ss.format.CellFormat.getInstance(CellFormat.java:133)
at com.pawaa.conversion.exceltohtml.ExcelToHtmlConverter.printSheetContentImages(ExcelToHtmlConverter.java:572)
实际上,当我调试时,我发现在CellFormat类的方法中引起了异常。
当调用cf.apply(cell)方法时,我得到了NullPointerException,因为posNumFmt实例变量为null。
private CellFormatPart getApplicableFormatPart(Object value) {
if (value instanceof Number) {
double val = ((Number) value).doubleValue();
if (formatPartCount == 1) {
if (!posNumFmt.hasCondition()
|| (posNumFmt.hasCondition() && posNumFmt.applies(val))) {
return posNumFmt;
} else {
return new CellFormatPart("General");
}
} else if (formatPartCount == 2) {
if ((!posNumFmt.hasCondition() && val >= 0)
|| (posNumFmt.hasCondition() && posNumFmt.applies(val))) {
return posNumFmt;
} else if (!negNumFmt.hasCondition()
|| (negNumFmt.hasCondition() && negNumFmt.applies(val))) {
return negNumFmt;
} else {
// Return ###...### (255 #s) to match Excel 2007 behaviour
return new CellFormatPart(QUOTE + INVALID_VALUE_FOR_FORMAT + QUOTE);
}
} else {
if ((!posNumFmt.hasCondition() && val > 0)
|| (posNumFmt.hasCondition() && posNumFmt.applies(val))) {
return posNumFmt;
} else if ((!negNumFmt.hasCondition() && val < 0)
|| (negNumFmt.hasCondition() && negNumFmt.applies(val))) {
return negNumFmt;
// Only the first two format parts can have conditions
} else {
return zeroNumFmt;
}
}
} else {
throw new IllegalArgumentException("value must be a Number");
}
}