0

public void write(int row1, int cell1, String message, String sheet1) throws Throwable {

    String path = ".\\resources\\APItestdata.xlsx";
    FileInputStream fis = new FileInputStream(path);
    //File fis=new File(path);
    Workbook wb = WorkbookFactory.create(fis);
    Sheet sh = wb.getSheet(sheet1);
    Row r = sh.getRow(row1);
    Cell c = r.createCell(cell1);
    c.setCellValue(message);
    FileOutputStream fos = new FileOutputStream(path);
    wb.write(fos);
    wb.close();
}
p.k
  • 51
  • 4

1 Answers1

1

The stack trace tells you what code line throws the NullPointerException. So what code line is ExcelLibrary.java:45, code line 45 in ExcelLibrary.java?

I suspect it is Cell c = r.createCell(cell1);. So r is null. That's possible because Sheet.getRow returns the row representing the row-number or null if its not defined on the sheet.

So do some null checks:

...
    Sheet sh = wb.getSheet("sheet1");
    if (sh == null) sh = wb.createSheet("sheet1");
    Row r = sh.getRow(row1);
    if (r == null) r = sh.createRow(row1);
    Cell c = r.createCell(cell1);
    c.setCellValue(message);
...
Axel Richter
  • 56,077
  • 6
  • 60
  • 87