且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 Apache POI 库从 Excel (xlsx) 文件中读取会引发 NullPointerException

更新时间:2022-02-11 23:04:12

ClassLoaderResourceLoader.该行是:

return _classLoader.getResourceAsStream(resourceName);

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

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

ClassLoaderResourceLoader 对象是在 SchemaTypeSystemImpl:

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 来表示 bootstrap 类装载机.如果这样,则此方法将在此类实现中返回 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 Entries 中,这意味着它们由引导类加载器加载.正如我在评论中所说(您似乎忽略了),jar 的正确位置是应用程序的 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.