-2

This is the code what i have written. but when i am trying to print key and value its getting null pointer.

My main concern is to load the keys and values from the property files and need to store in a map

Map<String, String> prop1 = new HashMap<String, String>();
Map<String, String> prop2 = new HashMap<String, String>();

public static void main(String args[]) {

    Sample sample = new Sample();
    sample.getLocaleOne();
}

public void getLocaleOne() {
    Properties prop = new Properties();
    InputStream input = null;

    try {
        File file = new File("Labels.properties");
        System.out.println("file.getAbsolutePath());
        input = this.getClass().getClassLoader().getResourceAsStream("C:/workspaceNewI18NChnages/customer/Labels.properties");

        prop.load(input);

        Enumertion<?> e = prop.propertyNames();
        while (e.hasMoreElements()) {
            String key = (String) e.nextElement();
            String value = prop.getProperty(key);
            System.out.println("Key : " + key + ", Value : " + value);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Tim Biegeleisen
  • 387,723
  • 20
  • 200
  • 263
  • 2
    never post code as picture. always include it in question – XtremeBaumer Nov 23 '16 at 15:35
  • Actually, this isn't really a routine null pointer problem, rather he is using the wrong type of stream to load his properties file. – Tim Biegeleisen Nov 23 '16 at 15:40
  • getResourceAsStream is for loading resources that are available on the classpath but you are providing it with an absolute file path. Have a look at http://www.javaworld.com/article/2077352/java-se/smartly-load-your-properties.html – haggisandchips Nov 23 '16 at 15:41
  • See also http://stackoverflow.com/questions/8775303/read-properties-file-outside-jar-file – Tunaki Nov 23 '16 at 15:48

2 Answers2

0

Because you have an absolute path to your properties file, you should be using a FileInputStream, not a resource stream:

Properties prop = new Properties();
String filename = "C:\\workspaceNewI18NChnages\\customer\\Labels.properties";
FileInputStream in = new FileInputStream(filename);
props.load(in);
Tim Biegeleisen
  • 387,723
  • 20
  • 200
  • 263
0

If you want to read a properties file, just use a java.util.ressourceBundle like this :

ResourceBundle bundle = ResourceBundle.getBundle("properties.login");
System.out.printLn(bundle.getString("login"));// read login property
System.out.printLn(bundle.getString("pwd"));//   read pwd property

The file login.properties is in this case located in the package properties

kevin ternet
  • 3,779
  • 2
  • 14
  • 23