[英]NullPointerException trying to load a file
Scenario is the following: 方案如下:
src
folder src
文件夹中的包中 Resources
folder are two files: menu.json
and TestWS.properties
Resources
文件夹中有两个文件: menu.json
和TestWS.properties
So, I made a java class that should read menu.json
and return a menu (in html) based on its structure. 因此,我做了一个Java类,应该读取
menu.json
并根据其结构返回一个菜单(以html格式)。
When, inside the class, I do this 什么时候在课堂上
InputStream i =
Thread.currentThread().getContextClassLoader().
getResourceAsStream("Resources/menu.json");
BufferedReader r = new BufferedReader(new InputStreamReader(i));
The InputStreamReader
constructor throws a NullPointerException
, I suppose because it can't find the file. 我想
InputStreamReader
构造函数将抛出NullPointerException
,因为它找不到文件。
Funny thing is, if I try to load TestWS.properties
instead, it loads it just well. 有趣的是,如果我尝试加载
TestWS.properties
,它将很好地加载它。
I tried checking the project's properties, manually added both files under Project Source Path -> Resources
, adding the .json
extension to Compiler -> Copy file types to output directory
, renamed, deleted, recreated, changed extension to the json file, deployed to WAR and loaded the project on another machine running weblogic, but to no avail. 我尝试检查项目的属性,将两个文件手动添加到“
Project Source Path -> Resources
,将.json
扩展名添加到Compiler -> Copy file types to output directory
,重命名,删除,重新创建,将扩展名更改为json文件,并部署到WAR并将项目加载到另一台运行weblogic的计算机上,但无济于事。
Is there anything else I'm missing? 还有什么我想念的吗?
This is explained in this article . 这在解释这篇文章 。
In short, you have to change the compiler setting "Copy File Types to Output Directory" to include the extension of the resources you want to have available at runtime. 简而言之,您必须更改编译器设置“将文件类型复制到输出目录”,以包括希望在运行时可用的资源的扩展名。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.