0

I would like to read a property file, like this:

Properties props = new Properties();
props.load(MajorBot.class.getResourceAsStream("application.properties"));

But when I am trying to do this I get an error:

Exception in thread "main" java.lang.NullPointerException: inStream parameter is null at java.base/java.util.Objects.requireNonNull(Objects.java:246) at java.base/java.util.Properties.load(Properties.java:406) at majorbot.MajorBot.main(MajorBot.java:13)

My app was created using IntelliJ, new Gradle/Java project. After creation, there was a resources directory allready there. I have created application.properties there, but I cannod read this file.

Is there a way to fix this?

hc0re
  • 1,806
  • 2
  • 26
  • 61
  • You can try [this solution](https://stackoverflow.com/a/15749281/14152955) this should work but I didn't tested – OffRange May 12 '21 at 21:29

1 Answers1

0

getResourceAsStream will try to find your file in a subfolder of resources that matches your current class’s package. If you want to access a file that is not in a subfolder, specify it as e.g. “/application.properties”.

RedCondor
  • 11
  • 1
  • Your suggestion is correct, but it doesn’t use the “current class”—it uses the package of the class whose `getResource` method is being called. – VGR May 13 '21 at 00:01