java - NullpointerException when trying to read XLSX file -
i have code open xlsx file using apache poi
file existingxlsx = new file("/app/app.xlsx"); system.out.println("file exists: " + existingxlsx.exists()); workbook workbook = workbookfactory.create(existingxlsx); when try execute this, following output
file exists: true java.lang.nullpointerexception @ org.apache.poi.xssf.usermodel.xssfworkbook.ondocumentread(xssfworkbook.java:270) @ org.apache.poi.poixmldocument.load(poixmldocument.java:159) @ org.apache.poi.xssf.usermodel.xssfworkbook.<init>(xssfworkbook.java:186) @ org.apache.poi.ss.usermodel.workbookfactory.create(workbookfactory.java:91) the file trying open can opened in excel , show data correctly, can poi read xlsx file?
here file breaks;
https://mega.co.nz/#!fjmwjqki!czihqgmvpxoqdtxzsnb3ufyskbx4yftb03-li3ilmke
edit
i have tried, results in same error;
workbook workbook = new xssfworkbook(new fileinputstream(existingxlsx)); edit
i found line throwing exception on;
workbookdocument doc = workbookdocument.factory.parse(getpackagepart().getinputstream()); this.workbook = doc.getworkbook(); map<string, xssfsheet> shidmap = new hashmap<string, xssfsheet>(); for(poixmldocumentpart p : getrelations()) { if(p instanceof sharedstringstable) sharedstringsource = (sharedstringstable)p; else if(p instanceof stylestable) stylessource = (stylestable)p; else if(p instanceof themestable) theme = (themestable)p; else if(p instanceof calculationchain) calcchain = (calculationchain)p; else if(p instanceof mapinfo) mapinfo = (mapinfo)p; else if (p instanceof xssfsheet) { shidmap.put(p.getpackagerelationship().getid(), (xssfsheet)p); } } stylessource.settheme(theme); <== breaks here edit
after research poi seems unable find styles.xml , workbook.xml, find strange because simple reader textwrangler shows structure of archive shows me styles xml.
how fix this? there default styles.xml , workbook.xml can insert archive?
now i've dowloaded latest packages:
- poi-src-3.9-20121203.zip (as source)
- xmlbeans-2.6.0.zip
- jsr173_1.0_api.jar
- resolver.jar
- xbean.jar
- xbean_xpath.jar
- xmlbeans-qname.jar
- xmlpublic.jar
- ooxml-schemas-1.1.jar
- dom4j-1.6.1.jar
- commons-codec-1.8.jar
- commons-logging-1.1.3.jar
- ant.jar (ant 1.7)
and test2.xlsx read without problems:
public static void main(string arg []){ try { //file existingxlsx = new file("/app/app.xlsx"); file existingxlsx = new file("c:/java/poi-3.9/test-data/__theproblem/test2.xlsx"); system.out.println("file exists: " + existingxlsx.exists()); workbook workbook = workbookfactory.create(existingxlsx); } catch (exception e) { e.printstacktrace(); } } are sure you're using ooxml-schemas-1.1.jar poi documentation recommends?
edit
hmm. it's work me jar too.
i have downloaded poi-bin-3.9-20121203.tar.gz http://poi.apache.org/download.html
made new project in eclipse, extracted jars zip:
- lib/commons-codec-1.5.jar
- lib/commons-logging-1.1.jar
- lib/dom4j-1.6.1.jar
- lib/junit-3.8.1.jar
- lib/log4j-1.2.13.jar
- lib/poi-3.9-20121203.jar
- lib/poi-examples-3.9-20121203.jar
- lib/poi-excelant-3.9-20121203.jar
- lib/poi-ooxml-3.9-20121203.jar
- lib/poi-ooxml-schemas-3.9-20121203.jar
- lib/poi-scratchpad-3.9-20121203.jar
- lib/stax-api-1.0.1.jar
- lib/xmlbeans-2.3.0.jar
- add test xlsx:
- test-data/test2.xlsx
- the test java:
- src/xlsxreadtest1.java
source:
import java.io.file; import org.apache.poi.ss.usermodel.workbook; import org.apache.poi.ss.usermodel.workbookfactory; public class xlsxreadtest1 { public static void main(string arg []){ try { file existingxlsx = new file("c:/java/__work/apache_poi/poi-3.9-bin/test-data/test2.xlsx"); system.out.println("file exists: " + existingxlsx.exists()); workbook workbook = workbookfactory.create(existingxlsx); system.out.println("a1: " + workbook.getsheetat(0).getrow(0).getcell(0).getstringcellvalue()); } catch (exception e) { e.printstacktrace(); } } } run. (tried jdk1.7.0_07, jdk1.6.0_31)
result:
file exists: true a1: testing edit
"testing edit" content of first cell on first sheet of file.
i think, may try this, scratch.
(maybe using other jars project, whom interfere jars in class loader? class loader cunning guy...)
Comments
Post a Comment