0

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();
        }
        
          
        
        
    }
    
}
    

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Maybe, row(1) is null, you can try to create it first.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.