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 ?

Ashish Agarwal
  • 14,555
  • 31
  • 86
  • 125

7 Answers7

5

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");
c.s.
  • 4,786
  • 18
  • 32
  • Better explanation than me. +1. –  Aug 16 '13 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)` – Ashish Agarwal Aug 16 '13 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. Aug 16 '13 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 ``. I just removed conf from the code and it gives the exact same error. The code is `InputStream in = getClass().getClassLoader().getResourceAsStream("Config.properties");` – Ashish Agarwal Aug 16 '13 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. Aug 16 '13 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.

Community
  • 1
  • 1
  • `/Users/user...` would not work unless `Users` is in the classpath which I doubt. He is not loading files here – c.s. Aug 16 '13 at 19:50
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

Naveen Kumar Alone
  • 7,536
  • 5
  • 36
  • 57
0

Try with

getClass().getClassLoader().getResourceAsStream("conf/Config.properties");
adarshr
  • 61,315
  • 23
  • 138
  • 167
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.

Du-Lacoste
  • 11,530
  • 2
  • 71
  • 51
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"));
    }
}
  • 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](/help/how-to-answer). – Community May 11 '22 at 12:32