Search code examples
javajsonweblogicjdeveloper

NullPointerException trying to load a file


Scenario is the following:

  • Developing a jsp application with jdeveloper
  • Deploying to Oracle Weblogic
  • All files are in the same project
  • Source files are inside packages that are inside the src folder
  • Inside the Resources folder are two files: menu.json and TestWS.properties

So, I made a java class that should read menu.json and return a menu (in html) based on its structure.

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.

Funny thing is, if I try to load TestWS.properties instead, it loads it just well.

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.

Is there anything else I'm missing?


Solution

  • 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.