my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Problem adding new key/value pairs to Properties object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem adding new key/value pairs to Properties object" Watch "Problem adding new key/value pairs to Properties object" New topic
Author

Problem adding new key/value pairs to Properties object

J Solomon
Greenhorn

Joined: Oct 08, 2012
Posts: 28
Hello,

I am currently working with a Java Properties object to hold and modify some config values for my app. What I am currently doing is reading in a properties file that I have and using that to create a Java Properties object. I then need to set one of the values based on the parameter that I pass to my app. This works perfectly as long as that key/value pair exists in the properties file that I read in; however, if the key doesn't exist, I am not able to add that key value pair. From the docs that I've read, Properties.setProperty() should add that key value pair if it doesn't exist, but it is not working that way. Am I missing something? Any help would be greatly appreciated. I've also posted some of my code below:

Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4165
    
  21

And what does 'not working that way' mean? What do you see that is incorrect?


Steve
J Solomon
Greenhorn

Joined: Oct 08, 2012
Posts: 28
Steve Luke wrote:And what does 'not working that way' mean? What do you see that is incorrect?


If my original properties file does not have a 'OMNI_CT_ENABLED' key, and I run my app, then there is no 'OMNI_CT_ENABLED' key created. If the key exists in the original properties file, then the value will be updated according to the 'useBackDoor' parameter passed in.
J Solomon
Greenhorn

Joined: Oct 08, 2012
Posts: 28
J Solomon wrote:
Steve Luke wrote:And what does 'not working that way' mean? What do you see that is incorrect?


If my original properties file does not have a 'OMNI_CT_ENABLED' key, and I run my app, then there is no 'OMNI_CT_ENABLED' key created. If the key exists in the original properties file, then the value will be updated according to the 'useBackDoor' parameter passed in.


According to the javadocs, if the key does not exist, it will create the key and assign it to the value that you pass in, if no value is passed, the key will be assigned to NULL.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4165
    
  21

J Solomon wrote:
Steve Luke wrote:And what does 'not working that way' mean? What do you see that is incorrect?


If my original properties file does not have a 'OMNI_CT_ENABLED' key, and I run my app, then there is no 'OMNI_CT_ENABLED' key created. If the key exists in the original properties file, then the value will be updated according to the 'useBackDoor' parameter passed in.


My question is how do you know it isn't created? Do you open the file via Windows explorer? Which file (old or new)? Is it not set the next time you run this application? etc... Please, details about how you prove to yourself it is or is not working.

J Solomon wrote:According to the javadocs, if the key does not exist, it will create the key and assign it to the value that you pass in, if no value is passed, the key will be assigned to NULL.

Are you sure? Can you point me to the documentation you are using? This is the behavior I would expect (mostly) but the API doesn't seem to be clear, doesn't say what you say, and is in direct contradiction with you in regards to NULL values:
From the Java 7 API
public Object setProperty(String key,
String value)
Calls the Hashtable method put. Provided for parallelism with the getProperty method. Enforces use of strings for property keys and values. The value returned is the result of the Hashtable call to put.
Parameters:
key - the key to be placed into this property list.
value - the value corresponding to key.
Returns:
the previous value of the specified key in this property list, or null if it did not have one.
Since:
1.2
See Also:
getProperty(java.lang.String)

And the Hashtable#put(Object,Object) method
put
public V put(K key,
V value)
Maps the specified key to the specified value in this hashtable. Neither the key nor the value can be null.
The value can be retrieved by calling the get method with a key that is equal to the original key.

Specified by:
put in interface Map<K,V>
Specified by:
put in class Dictionary<K,V>
Parameters:
key - the hashtable key
value - the value
Returns:
the previous value of the specified key in this hashtable, or null if it did not have one
Throws:
NullPointerException - if the key or value is null
See Also:
Object.equals(Object), get(Object)

Though setProperty() doesn't specifically say it doesn't accept null values, it does say it uses put() and put() says it doesn't accept null values. And nowhere does it say what happens when the key doesn't already exist (like I said, I expect it to behave like you said, but you said it is in the API and I don't see it. I checked back to Java 1.5 and saw no change in wording.)

A few more things to check: are you sure the value for useBackDoor is what you think it is (either "Yes" or "No")? Have you printed it out or used a debugger to be sure?
J Solomon
Greenhorn

Joined: Oct 08, 2012
Posts: 28
Steve Luke wrote:My question is how do you know it isn't created? Do you open the file via Windows explorer? Which file (old or new)? Is it not set the next time you run this application? etc... Please, details about how you prove to yourself it is or is not working.


First scenario: I start with my original properties file, it has no 'OMNI_CT_ENABLED' key. I run my app and write a new file with updated properties, use Windows explorer to make sure the new file was created, and then use notepad to view contents of new properties file. There is no 'OMNI_CT_ENABLED' key in the file.

Next scenario: I start with my original properties file, that has 'OMNI_CT_ENABLED' key with value "FALSE". I run my app with 'no' passed as parameter, then use Windows explorer to make sure the new file was created, and then use notepad to view contents. Value of 'OMNI_CT_ENABLED' has been updated to "TRUE" in the file.


If I start with no properties file and run the following code, then I end up with a properties file that has the appropriate keys created, which leads me to believe that setProperty() will create the key if it doesn't exist.



I did misread the javadocs and posted incorrect info above and I do apologize for that.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7520
    
  18

J Solomon wrote:First scenario: I start with my original properties file, it has no 'OMNI_CT_ENABLED' key. I run my app and write a new file with updated properties, use Windows explorer to make sure the new file was created, and then use notepad to view contents of new properties file. There is no 'OMNI_CT_ENABLED' key in the file.

Well, one thing that's worth noting is that the docs for store() say:
The output stream remains open after this method returns.
and I don't see anywhere that you close() it.

I'm not sure if it's the problem, but it doesn't seem right.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
J Solomon
Greenhorn

Joined: Oct 08, 2012
Posts: 28


So I added a print line just cause I wanted to see what keys were actually in the Properties object before I write out to file, and it doesn't look like that key is even added to the Properties object in memory. Again, this is only an issue if the OMNI_CT_ENABLED key does not exist in the original properties file. Any ideas?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4165
    
  21

Have you done the println to prove what the useBackDoor variable holds?
J Solomon
Greenhorn

Joined: Oct 08, 2012
Posts: 28
Figured this out, it was my own stupidity. Thanks again for all the help.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4165
    
  21

J Solomon wrote:Figured this out, it was my own stupidity. Thanks again for all the help.

Can you post the fix so we can all learn from it please?
J Solomon
Greenhorn

Joined: Oct 08, 2012
Posts: 28
Steve Luke wrote:
J Solomon wrote:Figured this out, it was my own stupidity. Thanks again for all the help.

Can you post the fix so we can all learn from it please?


The issue was with my input, I should have normalized it so I didn't hit issues with the "if statement".
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Problem adding new key/value pairs to Properties object
 
Similar Threads
Confused about Inheritance (I think)
Threads: Multiple Readers and One writer
How to divide File in Java
new to socket programming
How to Remove Main()