java.lang.NullPointerException: Cannot invoke method getCell() on null object


#1

Trying to write to excel file from the code in this website - https://docs.katalon.com/katalon-studio/docs/write-data-into-excel-file-at-runtime-during-test-execution.html

But encounter the following error:

“Data_fromCell = getCell(1).getStringCellValue() FAILED.
Reason:
java.lang.NullPointerException: Cannot invoke method getCell() on null object
at AutoTest_Trial.run(AutoTest_Trial:15)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:331)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:322)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:301)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:293)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:227)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:114)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:105)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at TempTestCase1555042548584.run(TempTestCase1555042548584.groovy:21)”

Any idea what is wrong?


#2

Hello, I am pasting the code here to have everything on the same place.

FileInputStream file = new FileInputStream (new File("E:\\Testdata.xlsx"))
XSSFWorkbook workbook = new XSSFWorkbook(file);
XSSFSheet sheet = workbook.getSheetAt(0);

'Read data from excel'
String Data_fromCell=sheet.getRow(1).getCell(1).getStringCellValue();
'Write data to excel'
sheet.getRow(1).createCell(1).setCellValue("Mahesh2");

file.close();
FileOutputStream outFile =new FileOutputStream(new File("E:\\Testdata.xlsx"));
workbook.write(outFile);
outFile.close();

The exception says that you are calling getCell() on null object. I’d say your sheet object is either null or empty. Can you check it?

String Data_fromCell=sheet.getRow(1).getCell(1).getStringCellValue();


#3

It is not empty


#4

And is the path to your file correct?


#5

Same problem to me but wanna write in excel