I want to read two integer values 100 and 200 in cell A1 and A2 using Intellij with Apache Poi.
Issue: I am getting java.lang.nullpointerexception at the line num1 below.
I've checked that I have apache poi jars added and correct import directives added. Excel row and column index starts at 1. Hence, getRow(1).getCell(1) refers to cell A1. Also, getSheetAt(0) refers to the first and only sheet that I have in my excel file.
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import java.io.File;
import java.io.FileInputStream;
import java.lang.Exception;
int num1, num2, sum1;
File src=new File("C:/test/testdata.xlsx");
FileInputStream fis=new FileInputStream(src);
XSSFWorkbook wb=new XSSFWorkbook(fis);
XSSFSheet sheet = wb.getSheetAt(0);
num1 = Integer.parseInt(sheet.getRow(1).getCell(1).getStringCellValue());
num2 = Integer.parseInt(sheet.getRow(2).getCell(1).getStringCellValue());
sum1 = num1 + num2;
System.out.println("The sum: " + sum1);
When running the code successfully, I should see num1 and num2 populated from excel file, and the sum is printed in the console.