1

Why is the code below returning null, instead of true?

I can see that the property is being set based on the {TEST=true} output.

Java code:

import java.util.Properties;

public class Test {

    public static void main(String[] args) {
        System.out.println("1");
        Properties props = new Properties();
        props.put("TEST", true);
        System.out.println(props);
        System.out.println(props.getProperty("TEST"));
        System.out.println("2");
    }
}

Program output:

1
{TEST=true}
null
2
bobbyrne01
  • 6,295
  • 19
  • 80
  • 150
  • 6
    Use setProperty() instead of put(). getProperty() and setProperty() operate on Strings. See the JavaDoc, here: https://docs.oracle.com/javase/9/docs/api/java/util/Properties.html#setProperty-java.lang.String-java.lang.String- – Riaan Nel Feb 27 '20 at 12:50

3 Answers3

2

Use setProperty() instead of put(). getProperty() and setProperty() operate on Strings. See the JavaDoc, here: https://docs.oracle.com/javase/9/docs/api/java/util/Properties.html#setProperty-java.lang.String-java.lang.String-

If you look at the source code for the Properties class, you should see that it does an instanceof check on the value of the property that it retrieves in getProperty(). If the property value is not a String, it returns null.

Riaan Nel
  • 2,425
  • 11
  • 18
  • 1
    While everything you’ve said is true, people should not decide how to use methods based on source code. Method implementations are private and may change from one release to another. What is stable and will never change is the contract of the method, that is, its javadoc. – VGR Feb 27 '20 at 15:52
  • 1
    Fair comment - I'm definitely not advocating that anyone look at source code rather than public API's to make decisions. The only reason I cited the source is because the JavaDoc isn't particularly clear on this behaviour - so it simply answers the OP's original "why does this happen?" question. – Riaan Nel Feb 28 '20 at 06:07
1

The put method you use is taken from HashTable which Properties class is extending. If you want to use put then to retrieve it you should use get :

props.get("TEST");

However as mentioned in the comments, for setting properties you should be using setProperty() method instead :

props.setProperty("TEST", "true");
nullPointer
  • 4,419
  • 1
  • 15
  • 27
0

Because Properties inherits from Hashtable, the put and putAll methods can be applied to a Properties object. Their use is strongly discouraged as they allow the caller to insert entries whose keys or values are not Strings. The setProperty method should be used instead. If the store or save method is called on a "compromised" Properties object that contains a non-String key or value, the call will fail. Similarly, the call to the propertyNames or list method will fail if it is called on a "compromised" Properties object that contains a non-String key.

So for String use setProperty() and getProperty() method and for Object use put() and get() method.

chaitanya dalvi
  • 1,539
  • 2
  • 17
  • 25