title: Null pointer for blank columns and rows using Apache POI
body: <p>I need help regarding a recurring problem. Earlier I asked a similar question but it was partially answered. Question <a href="https://stackoverflow.com/questions/14227108/getting-null-pointer-exception-in-creating-cell-in-apache-poi">here</a>
  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.</p>
  
  <pre><code>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");
                  }
              }
          }
      }
  </code></pre>
  
  <p>I went on to research the problem and I saw that they're using MissingCellPolicy indicated <a href="https://stackoverflow.com/questions/4929646/how-to-get-an-excel-blank-cell-value-in-apache-poi">here</a> 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 </p>
  
  <h2>UPDATE 1</h2>
  
  <p>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:</p>
  
  <pre><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");
                  }
  
              }
          }
      }
  </code></pre>
  
  <h2>isRowEmpty</h2>
  
  <pre><code>public boolean isRowEmpty(Row row){
  
          if (row == null){
              return true;
          }
          else {
              return false;
          }
      }
  </code></pre>
  
  <h2>Update 2</h2>
  
  <p>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</p>
  
  <pre><code>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");
                  }   
  
              }
          }
      }
  </code></pre>