1

I have created a properties file in the location conf/Config.properties. The folder is under the project's root folder in Eclipse. I also added this to the .classpath.

I am reading the data from this file using the code:

InputStream in = getClass().getResourceAsStream("conf/Config.properties");
Properties properties = new Properties();
properties.load(in);
String fromEmail = properties.getProperty("emailID");
System.out.println("from email is " + fromEmail);
String fromEmailPass = properties.getProperty("emailPass");
String host = properties.getProperty("host");

This gives the error :

java.lang.NullPointerException
    at java.util.Properties$LineReader.readLine(Properties.java:418)
    at java.util.Properties.load0(Properties.java:337)
    at java.util.Properties.load(Properties.java:325)
    at com.sendum.integration.activities.notifications.ActivityImplSendActivationEmail.activateEmail(ActivityImplSendActivationEmail.java:23)

How do I read the data from the .properties file ?

2
  • 3
    Try "./conf/Config.properties" as the path.
    – Kayaman
    Commented Aug 16, 2013 at 19:10
  • 1
    Are you sure your path is correct? and InputStream is set properly?
    – erencan
    Commented Aug 16, 2013 at 19:11

7 Answers 7

6

getClass().getResourceAsStream("conf/Config.properties"); tries to load the resource from a path that is relative to your class location.

Either use:

  • getClass().getResourceAsStream("/conf/Config.properties"); (note the leading / this is an absolute path) or
  • getClass().getClassLoader().getResourceAsStream("conf/Config.properties"); (Note here you are using absolute path but no leading / is required)

Edit: I am confused of what your directory structure and your classpath are. By your comment I understand now that your folder structure is:

<Project folder>
   - src/
       // .java files
   - conf/
       Config.properties

You are saying that you have added conf to your classpath. So I understand that you have two source folders in Eclipse. If this is the case then both src and conf are your root package and you should change the above commands like below:

  • getClass().getResourceAsStream("/Config.properties"); or
  • getClass().getClassLoader().getResourceAsStream("Config.properties");
5
  • Better explanation than me. +1.
    – user2638922
    Commented Aug 16, 2013 at 19:23
  • This gives me a null pointer exception: at java.util.Properties$LineReader.readLine(Properties.java:418) at java.util.Properties.load0(Properties.java:337) at java.util.Properties.load(Properties.java:325) at com.sendum.integration.activities.notifications.ActivityImplSendActivationEmail.activateEmail(ActivityImplSendActivationEmail.java:23) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) Commented Aug 16, 2013 at 20:18
  • The conf folder should be in your root package i.e. in eclipse under src folder. Otherwise you should add it to your classpath. If you did added conf to your classpath then from the examples in my answer remove conf/
    – c.s.
    Commented Aug 16, 2013 at 20:30
  • I added the conf folder to the root folder (one level above the src folder) and also added this folder to the classpath. I see this entry in the .classpath file <classpathentry kind="src" path="conf"/>. I just removed conf from the code and it gives the exact same error. The code is InputStream in = getClass().getClassLoader().getResourceAsStream("Config.properties"); Commented Aug 16, 2013 at 20:37
  • @AshishAgarwal I just updated my answer with that information. If your structure is what you say that it is, this should work. As a test try to put your Config.properties inside src and run the same approach using the classloader as in your last comment. Otherwise you have a typo on the file name perhaps
    – c.s.
    Commented Aug 16, 2013 at 20:41
3

It seems that getClass().getResourceAsStream("conf/Config.properties"); is returning null. this means that currently it isn't finding the file.

Try using, getReasourceAsStream("./conf/Config.properties"). This is relative path because it starts in the current directory, then finds conf/Config.properties or try using the absolute path, getReasourceAsStream("/Users/user/filepath/conf/Config.properties")

See here for a similar post.

1
  • /Users/user... would not work unless Users is in the classpath which I doubt. He is not loading files here
    – c.s.
    Commented Aug 16, 2013 at 19:50
0

Try with

getClass().getClassLoader().getResourceAsStream("conf/Config.properties");
0

In your stream path it tries to load the resource from relative path to your class location.

Change your InputStream as

InputStream in = getClass().getResourceAsStream("../conf/Config.properties");

Or otherwise give the full path of your file location in your InputStream

0

Try the below code for fetching the data from the properties file.

Properties prop = new Properties();
InputStream input = null;

input = this.getClass().getClassLoader().getResourceAsStream("conf/Config.properties");

// load a properties file
prop.load(input);

String str = prop.getProperty("key");//like userName
0

You can do the following as well.

public static void loadPropertiesToMemory() {
    Properties prop = new Properties();
    InputStream input = null;
    String filePathPropFile = "configuration.properties";
    try {
        input = App.class.getClassLoader().getResourceAsStream(filePathPropFile);
        prop.load(input);
    } catch (IOException ex) {
        ex.printStackTrace();
    } 
}

App is the class which consists the method of loading property file. This way uses getResourceAsStream() method of java.lang.ClassLoader class. This method takes the path of the file (or resource) to be loaded. This method considers all paths as absolute. That is, if you provide only the name of file, it will search the file inside project folders such as conf, src.

Please note that the path given to this method should NOT begin with ‘/’ as the path is considered as implicitly absolute.

0
package com.core;
 
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
 
public class MyPropWithinClasspath {
 
    private Properties prop = null;
     
    public MyPropWithinClasspath(){
         
        InputStream is = null;
        try {
            this.prop = new Properties();
            is = this.getClass().getResourceAsStream("info.properties");
            prop.load(is);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
     
    public String getPropertyValue(String key){
        return this.prop.getProperty(key);
    }
     
    public static void main(String a[]){
         
        MyPropWithinClasspath mpc = new MyPropWithinClasspath();
        System.out.println("db.host: "+mpc.getPropertyValue("account.no"));
        System.out.println("db.user: "+mpc.getPropertyValue("pin.no"));
        System.out.println("db.password: "+mpc.getPropertyValue("phone.no"));
    }
}
1
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented May 11, 2022 at 12:32

Not the answer you're looking for? Browse other questions tagged or ask your own question.