poi读取excel
我用poi读取excel内容,
for(int firstNum = 3; firstNum < endRowNum;firstNum++)
{
1 HSSFRow row = sheet.getRow(firstNum);
2 if(row == null)
3 {
4 System.out.println("row is null!");
5 continue;
6 }
7 System.out.println(row.getRowNum());
8 HSSFCell lagcell = row.getCell(1);
if(lagcell == null)
{
System.out.println("lagcell is null!");
continue;
}
HSSFCell middlecell = row.getCell(2);
HSSFCell rowTwpeNameCell = row.getCell(4);
if(getCellValue(lagcell).trim() != null)
{
String writeString = "if (!= null &&) option=;";
writeTxtFile(writeString);
writeTxtFile("{");
continue;
}
为什么第8行会报
空指针呢,前面获取到的行不为空,为什么获取到的列却为空,不明白。
------解决方案--------------------
行不为空不等于列不为空,行里前面几列为空,后面的列有数据这很正常,所以在判断完行不为空后,取列的值的时候,要保证取数安全,就先判断列是否为空:
HSSFCell lagcell;
if(row.getCell(1)!=null)
lagcell = row.getCell(1);
else
lagcell="默认值";