-4

I am load the properties file and get the value from that file but when i using "Properties" class and getProperty(key) method, it was return null value.

code:

public class LoadPropertiesFile {

public static String getProperty (String key, String filePath) {
    Properties properties = new Properties();
    InputStream inputStream = null;
    String value = null;
    try {
        String appHome = ConfigUtil.getApplicationHome() + filePath; 
        inputStream = new FileInputStream(appHome);

        //load a properties file
        properties.load(inputStream);

        //get the property value 
        System.out.println(properties.getProperty("7"));   //print **Unlock**
        System.err.println(key);   //print **7**
        System.out.println(value);   //print **null**
        value = properties.getProperty(key);

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch(IOException e) {
                e.printStackTrace();
            }
        }
    }
    return value;
}
}

Output:

Unlock
7 
null

Property File:

2=Interactive
3=Network
4=Batch
5=Service
7=Unlock
8=Network Cleartext
10=Remote Desktop
11=Logon with cached credentials

call method:

logonType = new LoadPropertiesFile().getProperty("7", "path");

When i call that method it will return null value only. please help me guys.

Andrei Stefan
  • 51,654
  • 6
  • 98
  • 89
kannan arumugam
  • 41
  • 1
  • 2
  • 7

2 Answers2

3

You are initalizing value with null.

String value = null;

And you assign it after printing it:

System.out.println(value);
value = properties.getProperty(key);

Output: null

So value can only be null when you print it, as you never change its value until System.out.println(value);. Just switch those two statements:

value = properties.getProperty(key);
System.out.println(value);

Output: unlock

Edit

properties.getProperty(key) may return null too, but only if there is no such key in its table, otherwise it will return the assigned value, in your example unlock.

See the API Documentation on this for more details:

public String getProperty(String key)

Searches for the property with the specified key in this property list. If the key is not found in this property list, the default property list, and its defaults, recursively, are then checked. The method returns null if the property is not found.

http://docs.oracle.com/javase/7/docs/api/java/util/Properties.html#getProperty(java.lang.String)

Community
  • 1
  • 1
flotothemoon
  • 1,882
  • 2
  • 20
  • 37
1
System.out.println(value);   //print **null**
value = properties.getProperty(key);

Switch these two lines and initialize value before printing it:

value = properties.getProperty(key);
System.out.println(value);   //print Unlock
BackSlash
  • 21,927
  • 22
  • 96
  • 136
  • will this method return `null`? – Braj Aug 01 '14 at 12:17
  • @user32118114 properties.getProperty(key) will return null if there is no such key, but in that case it will return "unlock". – flotothemoon Aug 01 '14 at 12:18
  • @user3218114 It will return null if(and only if) the key does not exist. – BackSlash Aug 01 '14 at 12:18
  • @BackSlash what your answer says. – Braj Aug 01 '14 at 12:18
  • as per the output method should not return null value. – Braj Aug 01 '14 at 12:19
  • @user3218114 I'm not getting the point. Can you be a little more clear? – BackSlash Aug 01 '14 at 12:20
  • `value = properties.getProperty(key);` will initialize the value and the returned value will never be null. What OP's last line says : When i call that method it will return null value only – Braj Aug 01 '14 at 12:20
  • @user3218114 Are you sure? What will happen if the key doesn't exist? `value` will be `null`. – BackSlash Aug 01 '14 at 12:21
  • @BackSlash Look what key is passed as argument and what is output. – Braj Aug 01 '14 at 12:23
  • @user3218114 Please, look at the code. `System.out.println(value);` is **before** `value` gets initialized, so it will print out `null`. It's what my answer states, and it's why the OP is getting `null`. – BackSlash Aug 01 '14 at 12:25
  • @BackSlash `System.out.println(properties.getProperty("7"));` what it is printing? `key="7"` passes. and `value = properties.getProperty(key);` – Braj Aug 01 '14 at 12:27
  • @user3218114 `System.out.println(value); value = properties.getProperty(key);` Do you see something wrong here? Let me show you something similar [in this demo](http://ideone.com/D3d19W), maybe you'll get the point – BackSlash Aug 01 '14 at 12:31
  • OK but I was more concerned about last statement. *When i call that method it will return null value only.* The method will not return `null` value, Am I right? – Braj Aug 01 '14 at 12:33
  • @user3218114 I really think that the OP is wrong. It can't happen with the scenario he described, it's ***impossible***. If the file exists and holds that content, everything will work just fine. – BackSlash Aug 01 '14 at 12:34