dimas
dimas

Reputation: 2597

Null pointer for blank columns and rows using Apache POI

I need help regarding a recurring problem. Earlier I asked a similar question but it was partially answered. Question here The problem is that everytime I wanted to write on a row that doesn't have any data or blank I always get a null pointer error problem. My temporary solution was to populate those rows with data so I can write something on each column. Also the solution given by one of the volunteers who helped me was only a temporary one. I made a few alterations regarding my code (below). Null pointer error points to the line with double asterisk.

public void writeSomething(XSSFWorkbook wb){
        for (String elem: listOfSheetNames){
            if (elem.equals("Sheet2")){

                sheet = wb.getSheet(elem);
                XSSFRow row = sheet.getRow(2);
                **XSSFCell cell = row.getCell(3, Row.CREATE_NULL_AS_BLANK );

                if (cell.getCellType() == Cell.CELL_TYPE_BLANK){
                    cell = row.createCell(3);
                    cell.setCellType(Cell.CELL_TYPE_STRING);
                    cell.setCellValue("this was blank");
                }
            }
        }
    }

I went on to research the problem and I saw that they're using MissingCellPolicy indicated here I went on to try that code but it doesn't work so I checked on Apache Docs and they have Row.Create_Null_as_Blank but am not sure if it is the same with MissingCellPolicy.Create_Null_as_Blank or the latter was just used in previous poi version. Although this should have solved the problem regarding columns or rows which are blank. I still got a null pointer exception error. I am using Apache poi version 3.9 btw if this helps. Any help is greatly appreciate. Thanks

UPDATE 1

I updated my code using jims solution. I added a method that will check if a row exists or not. But I still have the same error show with two asterisks. Here is my code:

public void writeSomething(XSSFWorkbook wb){
        for (String elem: listOfSheetNames){
            if (elem.equals("Sheet2")){
                int y=1;
                sheet = wb.getSheet(elem); 
                XSSFRow row = null; //create row variable outside if scope

                if (isRowEmpty(sheet.getRow(4))== false){
                    row = sheet.createRow(4);
                }   

                **XSSFCell cell = row.getCell(y, Row.CREATE_NULL_AS_BLANK );

                if (cell.getCellType() == Cell.CELL_TYPE_BLANK){
                    cell = row.createCell(y);
                    cell.setCellType(Cell.CELL_TYPE_STRING);
                    cell.setCellValue("Hooooy this was blank");
                }

            }
        }
    }

isRowEmpty

public boolean isRowEmpty(Row row){

        if (row == null){
            return true;
        }
        else {
            return false;
        }
    }

Update 2

I found a workaround for my code just in case someone would find it useful. I didn't use the row.getCell since I always encounter NUll pointer error. See code below

public void writeSomething(XSSFWorkbook wb){
        for (String elem: listOfSheetNames){
            if (elem.equals("Sheet2")){
                int y=1; //sets column number
                sheet = wb.getSheet(elem); 
                XSSFRow row = null; //create row variable outside if scope

                if (isRowEmpty(sheet.getRow(19))== false){
                    row = sheet.createRow(19);
                    XSSFCell cell = row.createCell(y);
                    cell.setCellType(Cell.CELL_TYPE_STRING);
                    cell.setCellValue("Hooooy this was blank");
                }   

            }
        }
    }

Upvotes: 2

Views: 12374

Answers (1)

Jim Garrison
Jim Garrison

Reputation: 86774

If the cell you need does not exist you must create it with

HSSFRow.createCell(int columnIndex, int type) 

Likewise, if the row does not exist you create it with

HSSFSheet.createRow(int rownum)

XSSF has identical methods as well.

Upvotes: 2

Related Questions