I am trying to read excel file using selenium but null pointer exception is displayed. I got the duplicate question i tried that user answer as well still i am not able to resolve it. Below is my code.
package demopackage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class demo1 {
public void readExcel(String filePath, String fileName, String sheetName) throws IOException {
// Create an object of File class to open xlsx file
File file = new File(filePath + "\\" + fileName);
// Create an object of FileInputStream class to read excel file
FileInputStream inputStream = new FileInputStream(file);
Workbook guru99Workbook = null;
// Find the file extension by splitting file name in substring and getting only
// extension name
String fileExtensionName = fileName.substring(fileName.indexOf("."));
// Check condition if the file is xlsx file
if (fileExtensionName.equals(".xlsx")) {
// If it is xlsx file then create object of XSSFWorkbook class
guru99Workbook = new XSSFWorkbook(inputStream);
}
// Check condition if the file is xls file
else if (fileExtensionName.equals(".xls")) {
// If it is xls file then create object of HSSFWorkbook class
guru99Workbook = new HSSFWorkbook(inputStream);
}
// Read sheet inside the workbook by its name
Sheet guru99Sheet = guru99Workbook.getSheet(sheetName);
// Find number of rows in excel file
int rowCount = guru99Sheet.getLastRowNum() - guru99Sheet.getFirstRowNum();
// Create a loop over all the rows of excel file to read it
for (int i = 0; i < rowCount + 1; i++) {
Row row = guru99Sheet.getRow(i);
// Create a loop to print cell values in a row
for (int j = 0; j < row.getLastCellNum(); j++) {
// Print Excel data in console
System.out.print(row.getCell(j).getStringCellValue() + "|| ");
}
System.out.println();
}
}
// Main function is calling readExcel function to read data from excel file
public static void main(String[] args) throws IOException {
// Create an object of ReadGuru99ExcelFile class
demo1 objExcelFile = new demo1();
// Prepare the path of excel file
String filePath = ("user.dir") + "\\src\\excelExportAndFileIO";
// Call read file method of the class to read data
objExcelFile.readExcel(filePath, "sheet1.xlsx", "ExcelGuru99Demo");
}
}
Output in console:
Exception in thread "main" java.lang.NullPointerException
at demopackage.demo1.ReadExcel(demo1.java:38)
at demopackage.demo1.main(demo1.java:59)
I tried everything but not able to resolve it also m trying it for the first time Thanks in advance
Aucun commentaire:
Enregistrer un commentaire