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
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
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
Step-5.
- Rerun your Java program
Step-6.
Observe result and you won’t see NullPointerException any more.
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.