Created attachment 31590 [details] excel sheet We have an xlsx file exported for a regular tool. The file when we try open it using poi, throws null pointer exception. But when the xlsx file is opened saved and closed then poi works fine.
Any chance you could post the stacktrace you get?
Exception in thread "main" java.lang.NullPointerException at org.apache.poi.xssf.usermodel.XSSFWorkbook.onDocumentRead(XSSFWorkbook.java:284) at org.apache.poi.POIXMLDocument.load(POIXMLDocument.java:159) at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:221) at CompareData.Compare_Data.read_file(Compare_Data.java:68) at CompareData.Compare_Data.main(Compare_Data.java:50) Line 67-69: FileInputStream fileIn2 = new FileInputStream(fn); XSSFWorkbook filename2 = new XSSFWorkbook(fileIn2); XSSFSheet sheet1 = filename2.getSheetAt(0);
I stumbled across the same in files produced by the Suunto Sports Watch export on movescount.com, when taking a closer look I saw that the latest trunk-version already has a fix via Bug 56278, so this one is actually a duplicate. *** This bug has been marked as a duplicate of bug 56278 ***
public static void main(String[] args) throws IOException { FileInputStream fileIn = new FileInputStream("C:/Lenovo/Lenovo_A.xlsx"); XSSFWorkbook filename = new XSSFWorkbook(fileIn); XSSFSheet sheet = filename.getSheetAt(0); String columnWanted = "Link"; Integer columnNo = null; List <Cell> cells = new ArrayList<Cell>(); Row firstRow = sheet.getRow(0); for (Cell cell:firstRow){ if(cell.getStringCellValue().equals(columnWanted)){ columnNo = cell.getColumnIndex(); } } if(columnNo != null){ for (Row row: sheet){ Cell c = row.getCell(columnNo); if(c == null || c.getCellType() == Cell.CELL_TYPE_BLANK){ } else { cells.add(c); } } } else { System.out.println("could not find column" + columnWanted + "in first row of" + fileIn.toString()); } } I have this code. Now I get Exception in thread "main" at line for (Cell cell:firstRow) with same excel file
I even tried to print all rows, it only prints the last row.
This file seems to be built in a way we do not expect at all, when I run the following: for(Row row : sheet) { for(Cell cell : row) { System.out.println("Cell in Row: " + row.getRowNum() + ": " + cell.toString()); } } It prints out the following, i.e. only one row with num "-1" is found where the last value in the column is returned, irrespective of the row in which it actually is, looks like all cells are added to the same "-1" row: Cell in Row: -1: ThinkPad X240 Cell in Row: -1: Intel® Core™ i5-4200U processor (2 cores, 1.60GHz, 3MB cache) Cell in Row: -1: Windows 8 Standard 64 - English Cell in Row: -1: 12.5" HD WXGA (1366 X 768) LED Backlight w/ 720p HD Camera Cell in Row: -1: Intel HD Graphics 4400 Cell in Row: -1: 4 GB PC3-12800 DDR3 SDRAM 1600MHz SODIMM Memory Cell in Row: -1: 500GB, 7200RPM Serial ATA 2.5" Hard Drive Cell in Row: -1: No Optical Included Cell in Row: -1: $761.60