使用Apache POI库从Excel(xlsx)文件读取会引发NullPointerException [英] Reading from an Excel (xlsx) file using Apache POI library throws NullPointerException

查看:94
本文介绍了使用Apache POI库从Excel(xlsx)文件读取会引发NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Eclipse IDE构建JSP Web应用程序.Web应用程序在Tomcat服务器版本7上加载没有问题.我们有一个新的要求,可以读取xls和xlsx文件.因此,我们已使用Apache POI库(4.1.2)来实现此目的,如下所示:我已经删除了其他代码,因为它们与本文无关.

I 'm building a JSP web application using Eclipse IDE. The web application loads without issue on tomcat server ver 7. We got a new requirement to read from xls and xlsx file. So we have used Apache POI library (4.1.2) to implement this as follows: I have taken out other codes as they are not relevant for this post

FileInputStream file = new FileInputStream(new File(args[0]));

        //Create Workbook instance holding reference to .xlsx file
        XSSFWorkbook workbook = new XSSFWorkbook(file); //<- This is where the exception occurs

        //Get first/desired sheet from the workbook
        XSSFSheet sheet = workbook.getSheetAt(0);

        //Iterate through each rows one by one
        Iterator<Row> rowIterator = sheet.iterator();

但是在执行时,我们得到以下异常.我曾尝试使用Google谷歌搜索,但未找到相关帮助.你能帮忙吗?

But when executing we are getting the below exception. I have tried googling but didn't find relevant help. Can you please assist?

SEVERE: Servlet.service() for servlet [UploadEstimates] in context with path [/AD] threw exception [Servlet execution threw an exception] with root cause
java.lang.NullPointerException
at org.apache.xmlbeans.impl.schema.ClassLoaderResourceLoader.getResourceAsStream(ClassLoaderResourceLoader.java:33)
at org.apache.xmlbeans.impl.schema.SchemaTypeSystemImpl$XsbReader.getLoaderStream(SchemaTypeSystemImpl.java:2249)
at org.apache.xmlbeans.impl.schema.SchemaTypeSystemImpl$XsbReader.<init>(SchemaTypeSystemImpl.java:1522)
at org.apache.xmlbeans.impl.schema.SchemaTypeSystemImpl.initFromHeader(SchemaTypeSystemImpl.java:273)
at org.apache.xmlbeans.impl.schema.SchemaTypeSystemImpl.<init>(SchemaTypeSystemImpl.java:185)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at schemaorg_apache_xmlbeans.system.sD023D6490046BA0250A839A9AD24C443.TypeSystemHolder.loadTypeSystem(Unknown Source)
at schemaorg_apache_xmlbeans.system.sD023D6490046BA0250A839A9AD24C443.TypeSystemHolder.<clinit>(Unknown Source)
at sun.misc.Unsafe.ensureClassInitialized(Native Method)
at sun.reflect.UnsafeFieldAccessorFactory.newFieldAccessor(Unknown Source)
at sun.reflect.ReflectionFactory.newFieldAccessor(Unknown Source)
at java.lang.reflect.Field.acquireFieldAccessor(Unknown Source)
at java.lang.reflect.Field.getFieldAccessor(Unknown Source)
at java.lang.reflect.Field.get(Unknown Source)
at org.apache.xmlbeans.XmlBeans.typeSystemForClassLoader(XmlBeans.java:775)
at org.openxmlformats.schemas.drawingml.x2006.main.ThemeDocument.<clinit>(Unknown Source)
at org.openxmlformats.schemas.drawingml.x2006.main.ThemeDocument$Factory.parse(Unknown Source)
at org.apache.poi.xssf.model.ThemesTable.<init>(ThemesTable.java:86)
at org.apache.poi.ooxml.POIXMLFactory.createDocumentPart(POIXMLFactory.java:61)
at org.apache.poi.ooxml.POIXMLDocumentPart.read(POIXMLDocumentPart.java:684)
at org.apache.poi.ooxml.POIXMLDocument.load(POIXMLDocument.java:180)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:288)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:342)
at com.MyClient.AD.util.UploadEstimateUT.start(UploadEstimateUT.java:85)
at com.MyClient.AD.util.UploadEstimates.doPost(UploadEstimates.java:107)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:754)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:492)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:165)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:1025)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:452)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1195)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:654)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:319)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)

这是我引用POI文件的方式:MyProject->右键单击->构建路径->配置构建路径->库选项卡,并添加以下jars

This is how I referenced the POI files: MyProject -> Right Click -> Build Path -> Configure Built Path -> Libraries tab and added the following jars

这就是我执行应用程序的方式运行->运行配置...->类路径选项卡->确保此处引用了所有jar文件,包括Apache POI->应用和/或运行.

And this is how I execute the application Run -> Run Configuration... -> Classpath tab -> Making sure all jar files are referenced here including Apache POI's -> Apply and/or Run.

更新1(2020年2月28日)

我尝试了与Java应用程序相同的代码(使用POI库从excel读取),并且按预期方式工作.

I tried the same code (that reads from excel using POI library) as java application and it worked as expected.

我在调试JSP Web应用程序时注意到的另一件事是,控件直接转移到了finally块,而不是常规的Exception块.不知道为什么.

Another thing I noted when debugging the JSP web application is that the control is transferred directly to the finally block instead of the general Exception block. Not sure why.

更新2(2020年3月3日)

我在同事系统中配置了该项目,该系统也具有Eclipse IDE,但其Tomcat服务器版本为9.0.31,并且可以在他的计算机中使用.

I configured the project in my colleagues system who also has Eclipse IDE but with Tomcat server version 9.0.31 and it worked in his machine.

然后,我安装了9.0.31版的Tomcat服务器,并从单独的工作区配置了该项目,但是在执行时,我遇到了相同的异常.我不明白有什么区别.

Then I installed Tomcat server version 9.0.31 and configured the project from a separate workspace but when executing I face the same exception. I'm not able to understand what could the difference.

我们俩都将Eclipse IDE用于企业Java开发人员.版本:2019-09 R(4.13.0)内部版本:20190917-1200

We both are using Eclipse IDE for Enterprise Java Developers. Version: 2019-09 R (4.13.0) Build id: 20190917-1200

更新3(2020年3月6日)

魔术师,我将其更新为出厂方法后得到了以下异常

magicmn, I get the below exception after updating to factory method

    SEVERE: Servlet.service() for servlet [UploadEstimates] in context with path [/AD] threw exception [Servlet execution threw an exception] with root cause
java.lang.NullPointerException
    at org.apache.xmlbeans.impl.schema.ClassLoaderResourceLoader.getResourceAsStream(ClassLoaderResourceLoader.java:33)
    at org.apache.xmlbeans.impl.schema.SchemaTypeSystemImpl$XsbReader.getLoaderStream(SchemaTypeSystemImpl.java:2249)
    at org.apache.xmlbeans.impl.schema.SchemaTypeSystemImpl$XsbReader.<init>(SchemaTypeSystemImpl.java:1522)
    at org.apache.xmlbeans.impl.schema.SchemaTypeSystemImpl.initFromHeader(SchemaTypeSystemImpl.java:273)
    at org.apache.xmlbeans.impl.schema.SchemaTypeSystemImpl.<init>(SchemaTypeSystemImpl.java:185)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at schemaorg_apache_xmlbeans.system.sD023D6490046BA0250A839A9AD24C443.TypeSystemHolder.loadTypeSystem(Unknown Source)
    at schemaorg_apache_xmlbeans.system.sD023D6490046BA0250A839A9AD24C443.TypeSystemHolder.<clinit>(Unknown Source)
    at sun.misc.Unsafe.ensureClassInitialized(Native Method)
    at sun.reflect.UnsafeFieldAccessorFactory.newFieldAccessor(Unknown Source)
    at sun.reflect.ReflectionFactory.newFieldAccessor(Unknown Source)
    at java.lang.reflect.Field.acquireFieldAccessor(Unknown Source)
    at java.lang.reflect.Field.getFieldAccessor(Unknown Source)
    at java.lang.reflect.Field.get(Unknown Source)
    at org.apache.xmlbeans.XmlBeans.typeSystemForClassLoader(XmlBeans.java:775)
    at org.openxmlformats.schemas.drawingml.x2006.main.ThemeDocument.<clinit>(Unknown Source)
    at org.openxmlformats.schemas.drawingml.x2006.main.ThemeDocument$Factory.parse(Unknown Source)
    at org.apache.poi.xssf.model.ThemesTable.<init>(ThemesTable.java:86)
    at org.apache.poi.ooxml.POIXMLFactory.createDocumentPart(POIXMLFactory.java:61)
    at org.apache.poi.ooxml.POIXMLDocumentPart.read(POIXMLDocumentPart.java:684)
    at org.apache.poi.ooxml.POIXMLDocument.load(POIXMLDocument.java:180)
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:288)
    at org.apache.poi.xssf.usermodel.XSSFWorkbookFactory.createWorkbook(XSSFWorkbookFactory.java:97)
    at org.apache.poi.xssf.usermodel.XSSFWorkbookFactory.createWorkbook(XSSFWorkbookFactory.java:125)
    at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:327)
    at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:295)
    at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:276)
    at com.MyClient.AD.util.UploadEstimateUT.start(UploadEstimateUT.java:90)
    at com.MyClient.AD.util.UploadEstimates.doPost(UploadEstimates.java:108)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:688)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:367)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1639)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)

推荐答案

这意味着_classLoader为空._classLoader传递给构造函数.

Which means that _classLoader is null. _classLoader is passed to the constructor.

ClassLoaderResourceLoader对象是在

The ClassLoaderResourceLoader object is created at line 182 of SchemaTypeSystemImpl:

_classloader = indexclass.getClassLoader();
...
_resourceLoader = new ClassLoaderResourceLoader(_classloader);

这意味着_classloader为null,这意味着indexclass.getClassLoader()为null.

Which means that _classloader is null, which means that indexclass.getClassLoader() is null.

getClassLoader()何时可以返回null?文档说:

When can getClassLoader() return null? The documentation says:

某些实现可能使用null来表示引导类装载机.在这种实现中,此方法将返回null该类由引导类加载器加载.

Some implementations may use null to represent the bootstrap class loader. This method will return null in such implementations if this class was loaded by the bootstrap class loader.

引导程序?它会响吗?是的,您将所有的jar放在Bootstrap条目中,这意味着它们是由bootstrap类加载器加载的.正如我在评论中说的那样(您似乎已经忽略了),罐子的正确位置是应用程序的WEB-INF \ lib目录.因此,只需将它们放在其中即可使用.

Bootstrap? Does it ring a bell? Yes, you put all your jars in the Bootstrap Entries, which means that they are loaded by the bootstrap class loader. As I said in a comment (which you seem to have ignored), the correct location for jars is the WEB-INF\lib directory of the application. So just put them in there and it will work.

这篇关于使用Apache POI库从Excel(xlsx)文件读取会引发NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆