简体   繁体   English

NullPointerException尝试加载文件

[英]NullPointerException trying to load a file

Scenario is the following: 方案如下:

  • Developing a jsp application with jdeveloper 用jdeveloper开发一个jsp应用程序
  • Deploying to Oracle Weblogic 部署到Oracle Weblogic
  • All files are in the same project 所有文件都在同一个项目中
  • Source files are inside packages that are inside the src folder 源文件位于src文件夹中的包中
  • Inside the Resources folder are two files: menu.json and TestWS.properties Resources文件夹中有两个文件: menu.jsonTestWS.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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM