How to add Resources Folder, Properties at Runtime into IntelliJ classpath? Adding Property files to Classpath

Last updated
App Shah

Crunchify » Technology & Tools » How to add Resources Folder, Properties at Runtime into IntelliJ classpath? Adding Property files to Classpath

NullPointerException while reading Resource .properties file in Intellij IDE

Getting NullPointerException while reading Resource .properties file in Intellij IDE?

Do you have an issue loading resources files into your Intellij IDE? Having below errors?

  • Error: Could not find or load main class in intelliJ IDE
  • IntelliJ IDEA – getClass().getResource(“…”) return null
  • Unable to find .properties file at runtime
  • Not able to find resource file in classpath?
  • How to add a property file to a project/module at runtime in Intellij IDE?

Today while running tutorial how to read config.properties file in Java at runtime I got below error in IntelliJ IDE. Program runs very well without any issue in Eclipse IDE.

Exception in thread "main" java.lang.NullPointerException: inStream parameter is null
at java.base/java.util.Objects.requireNonNull(Objects.java:247)
at java.base/java.util.Properties.load(Properties.java:404)
at crunchify.com.tutorials.CrunchifyGetPropertyValues.getPropValues(CrunchifyGetPropertyValues.java:25)
at crunchify.com.tutorials.CrunchifyReadConfigMain.main(CrunchifyReadConfigMain.java:13)

It took me some time to fix this in IntelliJ IDE as I’m new to JetBrain's IntelliJ 🙂

Basically IntelliJ was giving me NullPointerException as it was not able to find resource folder at runtime.

Here is a code block.

String propFileName = "config.properties";
		
InputStream inputStream = getClass().getClassLoader().getResourceAsStream(propFileName);

prop.load(inputStream);          <========= NPE here while loading config.properties file

if (inputStream == null) {
	throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
}

How to solve java.lang.NullPointerException: inStream parameter is null error?

Step-1.

  • Right click on project
  • Click on Mark Directory as
  • Click on Sources Root

IntelliJ IDE - Mark Project as Source Root - Crunchify Tips

Step-2.

  • Click on File
  • Click on Project Structure… to open settings panel

Step-3.

  • Click on Modules tab
  • As you see there isn’t any resources folder added as Content Root
  • We need to add resources folder into it

IntelliJ Click on Modules Tab and check added resources - Crunchify

Step-4.

  • Make sure to click on resource folder
  • Click on Resources tab after that
  • You will see resources folder added as Resource Folders in right panel

Add resources folder into resources tab - Crunchify

Step-5.

ReRun Crunchify Java Program - Crunchify

Step-6.

Observe result and you won’t see NullPointerException any more.

Config.properties file loaded successfully and no NPE in IntelliJ IDE - Crunchify

I hope this simple tips will help you fix NPE and easy for you to add any resources into your Java or J2EE project at runtime without any issue.

Happy coding and keep sharing.

8 thoughts on “How to add Resources Folder, Properties at Runtime into IntelliJ classpath? Adding Property files to Classpath”

  1. This solution is wicked cool! Why is IntelliJ hiding this feature in the deep recesses of the abyss, is beyond my wildest imaginings.

    Reply
    • Yeah. There are so many options out there in IntelliJ IDE and it’s pain to find out appropriate option for individual use.

      Any cheat sheet with list of all options would be a great from IntelliJ team.

      Reply
  2. Are you getting Gmail problems like IMAP server error or any such things?

    If you are frustrated with an IMAP Server Error or any other Gmail problems too, then, now it’s time to get your smile back with support. Technical support team is well talented and sufficient enough to take you out of such issues.

    They keep ears on every issue, and they become active to fix those issues when you command them at the toll-free number.

    Reply
    • Hi Kristen. Thanks for your comment. I see it’s all about promoting IMAP Server support 🙂

      Please try posting comment related to article.

      Reply

Leave a Comment