M Sohaib Khan
M Sohaib Khan

Reputation: 148

Changing the getROW index in apachee POI throw NullPointerException

I am trying to write to Excel using apachee POI. It works when the getrow index is 0 like[getrow(o)]. But changing it other than zero throw nullpointer exception.

package fairfoxchecking;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class writingInExcel {
    public static void main(String []args) throws IOException {

    File src = new File("D:/Etl_Bug_reporting_Template.xlsx");
    FileInputStream fis = new FileInputStream(src);
    XSSFWorkbook wb = new XSSFWorkbook(fis);
    XSSFSheet sheet1 =wb.getSheetAt(0);

    sheet1.getRow(0).createCell(5).setCellValue("cheasdfasdfasck1");
    sheet1.getRow(1).createCell(5).setCellValue("cheasdfasdfasck1");

    FileOutputStream fout = new FileOutputStream(src);
    wb.write(fout);
    wb.close();
}

Upvotes: 0

Views: 782

Answers (2)

ARN
ARN

Reputation: 175

Before you do create a cell, you have to ensure the row is created.

Try something like,

if(sheet1.getRow(rowIndex) == null)
      sheeet1.createRow(rowIndex)
sheet1.getRow(rowIndex).createCell(colIndex).setCellValue(stringVal);

Upvotes: 2

vins
vins

Reputation: 15370

As per this , getRow might return null if it is not defined. So you might have to create one before writing.

sheet1.createRow(index)


Check this lib - seems to be nice.

Upvotes: 1

Related Questions