Reputation: 5191
While Trying to write into Excel file getting Null pointer Exception
coming from line sheet.createRow(1).createCell(5).setCellValue("Pass");
Not getting why this error is coming :(
package com.qtpselenium.Test;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import com.qtpselenium.util.Xls_Reader;
public class ReturnTestCaseResult {
public static void main(String[] args) {
String path =System.getProperty("user.dir") + "\\src\\com\\qtpselenium\\xls\\suiteA.xlsx";
/* Xls_Reader xlsr = new Xls_Reader(System.getProperty("user.dir") + "\\src\\com\\qtpselenium\\xls\\suiteA.xlsx");
ReportDataSetResult(xlsr, "TestCaseA1", 3, "Pass" , path);*/
ReportDataSetResult("TestCaseA1", path);
}
public static void ReportDataSetResult( String TestCaseName , String path){
System.out.println(TestCaseName +"----"+ path);
try {
FileInputStream fileinp = new FileInputStream(path);
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.getSheet(TestCaseName);
sheet.createRow(1).createCell(5).setCellValue("Pass");
FileOutputStream fileout = new FileOutputStream(path);
workbook.write(fileout);
fileout.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Upvotes: 0
Views: 2531
Reputation: 955
You have used the no arg constructor to create the workbook:
XSSFWorkbook workbook = new XSSFWorkbook();
which means there are no sheets in the workbook. This means your sheet variable will be null. I think you want to pass the FileInputStream fileinp into the workbook constructor so that it reads from an existing file?
XSSFWorkbook workbook = new XSSFWorkbook(fileinp);
Otherwise you will need to create a sheet called TestCaseName in the workbook before you can start adding rows to it.
Upvotes: 1