Bug 56490 - When trying to open xlsx file exported from a tool, null pointer exception
Summary: When trying to open xlsx file exported from a tool, null pointer exception
Status: REOPENED
Alias: None
Product: POI
Classification: Unclassified
Component: XSSF (show other bugs)
Version: 3.10-FINAL
Hardware: PC All
: P2 normal (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-05-05 16:56 UTC by sam
Modified: 2014-05-16 06:20 UTC (History)
0 users



Attachments
excel sheet (2.97 KB, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet)
2014-05-05 16:56 UTC, sam
Details

Note You need to log in before you can comment on or make changes to this bug.
Description sam 2014-05-05 16:56:18 UTC
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.
Comment 1 Nick Burch 2014-05-05 16:57:52 UTC
Any chance you could post the stacktrace you get?
Comment 2 sam 2014-05-05 17:02:21 UTC
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);
Comment 3 Dominik Stadler 2014-05-14 20:19:32 UTC
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 ***
Comment 4 sam 2014-05-14 21:26:31 UTC
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
Comment 5 sam 2014-05-15 14:33:12 UTC
I even tried to print all rows, it only prints the last row.
Comment 6 Dominik Stadler 2014-05-16 06:20:04 UTC
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