获得空指针异常在Apache的POI创建细胞 [英] Getting null pointer exception in creating cell in Apache POI

查看:308
本文介绍了获得空指针异常在Apache的POI创建细胞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到一个空指针错误,每次我跑我的code(下图)的,它指出了两个星号指定的行。

I am getting a null pointer error everytime I ran my code (below) and it points to the line specified with two asterisk.

public void writeSomething(XSSFWorkbook wb){
        for (String elem: listOfSheetNames){
            if (elem.equals("Sheet2")){
                sheet = wb.getSheet(elem); //sheet is of type XSSFSheet
                XSSFRow row = sheet.getRow(0);
                **XSSFCell cell = row.getCell(1);

                if (cell == null){
                    cell = row.createCell(1);
                    cell.setCellType(Cell.CELL_TYPE_STRING);
                    cell.setCellValue("Apple");
                }                                       
            }
        }
    }

我是一个新到Apache POI和我只是想将数据写入在第二空白单元Excel工作表(Sheet2的)。难道我做错了吗?

I am a new to apache poi and am just trying to write data to a blank cell in a 2nd excel sheet (Sheet2). Did I do something wrong here?

推荐答案

不幸的是,电池是目前不是一个空白单元格。指定纸张上放置数据使得空白。您可能需要先创建单元格,然后检查它是否是一个空白细胞,而不是

Unfortunately, the cell is currently null not a blank cell. Placing data on the specified sheet makes it blank. You might want to create the cell first then check if it is a blank cell rather than null.

**XSSFCell cell = row.createCell(1);

if (cell == Cell.CELL_TYPE_BLANK){
   cell.setCellType(Cell.CELL_TYPE_STRING);
   cell.setCellValue("Apple");
}   

这篇关于获得空指针异常在Apache的POI创建细胞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆