Q:

Getting exception while reading xlsx file using POI jars in java

I am unable to read xlsx file using java, getting Exception as  at the line where I am creating an object XSSFWorkbook(fis); 
HashMap<Short, String> keyMap = new HashMap<Short, String>();
HashMap<String, String> valueMap = null;
ArrayList<HashMap<String, String>> rowList = new ArrayList<HashMap<String, String>>();
try {
    FileInputStream fis = new FileInputStream(fileName);
    String fileExtn = getFileExtension(fileName);

    Workbook xssfWorkBook ;
    HSSFWorkbook hssfWorkBook ;
    Sheet sheet=null;
    if (fileExtn.equalsIgnoreCase("xlsx"))
    {
        log.debug("Before creating WorkBook");
        xssfWorkBook = new XSSFWorkbook(fis);
        log.debug("xssfWorkBook object created");
        sheet = xssfWorkBook.getSheetAt(0);
    }else  if (fileExtn.equalsIgnoreCase("xls"))
    {
        hssfWorkBook = new HSSFWorkbook(fis);
        sheet = (Sheet) hssfWorkBook.getSheetAt(0);
    }
    Iterator<Row> rows = sheet.rowIterator();
    int rowCount = 0;
    while (rows.hasNext()) {
        if (rowCount == 0) {
            Row row = rows.next();
            Iterator<Cell> cells = row.cellIterator();
            short cellCounter = 1;
            while (cells.hasNext()) {
                Cell cell = cells.next();
                String cellValue = getCellValueAsString(cell);
                keyMap.put(cellCounter, cellValue);
                cellCounter++;
            }
        } else {
            valueMap = new HashMap<String, String>();
            Row row = rows.next();
            Iterator<Cell> cells = row.cellIterator();
            short cellCounter = 1;
            while (cells.hasNext()) {
                Cell cell = cells.next();
                String cellValue = getCellValueAsString(cell);
                valueMap.put(keyMap.get(cellCounter), cellValue);
                cellCounter++;
            }
            rowList.add(valueMap);
        }
        rowCount++;
    }
} catch (FileNotFoundException e) {
    e.printStackTrace();
    log.error("FileNotFoundException" + e);
    throw new Exception("FileNotFoundException while reading Excel file, Message <" + e.getMessage() + ">");
} catch (IOException e) {
    e.printStackTrace();
    log.error("IOException" +e);
    throw new Exception("IOException while reading Excel file, Message <" + e.getMessage() + ">");
}catch(Exception e){
    e.printStackTrace();
    throw new Exception("Exception while reading Excel file, Message <" + e.getMessage() + ">");
}

Getting below exception as stackTrace is :

java.lang.NullPointerException at 
  org.apache.poi.openxml4j.opc.OPCPackage.getPart(OPCPackage.java:562)
  at 
  org.apache.poi.POIXMLDocumentPart.(POIXMLDocumentPart.java:63)
  at  org.apache.poi.POIXMLDocument.(POIXMLDocument.java:58) at 
  org.apache.poi.xssf.usermodel.XSSFWorkbook.(XSSFWorkbook.java:186)
  at 
  com.covad.portlet.mars.util.ExcelReader.readExcel(ExcelReader.java:72)

A:

I had this issue before make sure that the file is saved as 
ExcelWorkbook(.xlsx) ,the issue may happen if you saved the file as 
Strict Open XML SpreadSheet(.xlsx),they both have the same extension but the internal structures may vary and POI seems does not support it;to solve the issue on the file re save it again as ExcelWorkbook(*.xlsx) .
I hope this may solve your issue.

