2

I have one properties file, name is XYZ.properties. I want to get details from this file. My property file location is D:\properties_file\XYZ.properties

For this i am using below code

import java.io.InputStream;
import java.util.Properties;

public class LoadProp {
    private static Properties prop = new Properties();
    public static Properties getProperties() throws Exception {
        if (prop.isEmpty()) {
            InputStream fis = LoadProp.class.getResourceAsStream("D:\\properties_file\\XYZ.properties");
            prop.load(fis);
        }
        return prop;
    }
}

public class demo extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private static Properties propFile;

    public void doPost(HttpServletRequest request, HttpServletResponse response) {
        try {
            propFile = LoadProp.getProperties();
            System.out.println(propFile.getProperty(Constants.URL));
        }
        catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }
}

But when i run this on prop.load(fis) line it gives me the following error

java.lang.NullPointerException
Ramkrishna Sharma
  • 6,961
  • 3
  • 42
  • 51
user3441151
  • 1,880
  • 6
  • 35
  • 79
  • Resources are not files, do not reside in the file system, do not have backslashes as directory separators, and do not have drive letters as part of their names. – user207421 Oct 19 '16 at 11:29

2 Answers2

2

Change this line:

InputStream fis = LoadProp.class.getResourceAsStream("D:\\properties_file\\XYZ.properties");

to

InputStream fis = new FileInputStream("D:\\properties_file\\XYZ.properties");

The getResourceAsStream is designed to read a resource from classpath (resource inside your app), in order to read file that is outside the app, use java File API.

Krzysztof Cichocki
  • 6,294
  • 1
  • 16
  • 32
0

Class.getResourceAsStream does not open a file system location. It tries to find the resource in the class path. You may want to read the properties with a FileInputStream instead.

Henry
  • 42,982
  • 7
  • 68
  • 84