United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-4921212 : XMLEncoder does NOT encode NULL (value) entries in HashMap

Details
Type:
Bug
Submit Date:
2003-09-11
Status:
Resolved
Updated Date:
2005-11-09
Project Name:
JDK
Resolved Date:
2005-11-09
Component:
client-libs
OS:
linux,windows_2000
Sub-Component:
java.beans
CPU:
x86
Priority:
P3
Resolution:
Fixed
Affected Versions:
1.4.0,1.4.2,1.4.2_01
Fixed Versions:

Related Reports
Relates:

Sub Tasks

Description
Name: gm110360			Date: 09/11/2003


FULL PRODUCT VERSION :
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1_01-b01)
Java HotSpot(TM) Client VM (build 1.4.1_01-b01, mixed mode)

FULL OPERATING SYSTEM VERSION :
Microsoft Windows 2000 [Version 5.00.2195] SP 3

A DESCRIPTION OF THE PROBLEM :
If you Encode a java.util.HashMap containing en entry with
a "Key" and "null" for the value, then this entry will not
be included in the XML output.



STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Create a new java.util.HashMap
2. Add some entries using the "put" method
3. Add one entry like the following put("Key1", null);
4. decode the map using the java.beans.XMLEncoder

EXPECTED VERSUS ACTUAL BEHAVIOR :
Expected result:
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.4.1_01" class="java.beans.XMLDecoder">
 <object class="java.util.HashMap">
  <void method="put">
   <string>Key4</string>
   <string>4</string>
  </void>
  <void method="put">
   <string>Key3</string>
   <null/>
  </void>
  <void method="put">
   <string>Key1</string>
   <string>1</string>
  </void>
  <void method="put">
   <string>Key5</string>
   <string>5</string>
  </void>
  <void method="put">
   <string>Key2</string>
   <string>2</string>
  </void>
  <void method="put">
   <null/>
   <string>6</string>
  </void>
  <void method="put">
   <string>Key7</string>
   <string>7</string>
  </void>
 </object>
</java>




Actual Result:
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.4.1_01" class="java.beans.XMLDecoder">
 <object class="java.util.HashMap">
  <void method="put">
   <string>Key4</string>
   <string>4</string>
  </void>
  <void method="put">
   <string>Key1</string>
   <string>1</string>
  </void>
  <void method="put">
   <string>Key5</string>
   <string>5</string>
  </void>
  <void method="put">
   <string>Key2</string>
   <string>2</string>
  </void>
  <void method="put">
   <null/>
   <string>6</string>
  </void>
  <void method="put">
   <string>Key7</string>
   <string>7</string>
  </void>
 </object>
</java>


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
		Map myMap;

		myMap = new HashMap();

		myMap.put("Key1", "1");
		myMap.put("Key2", "2");
		myMap.put("Key3", null);
		myMap.put("Key4", "4");
		myMap.put("Key5", "5");
		myMap.put(null, "6");
		myMap.put("Key7", "7");

		try {

			XMLEncoder encoder = new XMLEncoder(new
BufferedOutputStream(new FileOutputStream("c:\\Test.xml")));
			encoder.writeObject(myMap);
			encoder.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
---------- END SOURCE ----------
(Incident Review ID: 181400) 
======================================================================

                                    

Comments
EVALUATION

At comparison of contents of two maps it is necessary to check not only equality of values, but also presence of a keys if values are equal to null.  Nine possible combinations are listed in the table below.  Recommended actions are presented for each combination.  In the previous version the sixth combination was processed as the fifth one.
  -----------------------------
  | oldInstance | newInstance |
  | key | value | key | value |
  -----------------------------
1.| yes | !null | yes | !null | put( oldKey, oldValue ) if value changed
2.| yes | !null | yes |  null | put( oldKey, oldValue )
3.| yes | !null | no  |  null | put( oldKey, oldValue )
  -----------------------------
4.| yes |  null | yes | !null | put( oldKey, null )
5.| yes |  null | yes |  null |
6.| yes |  null | no  |  null | put( oldKey, null )
  -----------------------------
7.| no  |  null | yes | !null | remove( newKey )
8.| no  |  null | yes |  null | remove( newKey )
9.| no  |  null | no  |  null |
  -----------------------------
                                     
2005-10-14
EVALUATION

I have found out, that algorithm for checking the changes in the state of java.util.Map is wrong. If values returned for some key is null, then this algorithm considers that the condition has not changed. However, it is necessary to be convinced, that methods containsKey() return identical values. See line 283 in file java/beans/MetaData.java.
                                     
2005-10-11
CONVERTED DATA

BugTraq+ Release Management Values

COMMIT TO FIX:
dragon


                                     
2004-06-14
EVALUATION

the null value in the Map/HashMap is not honored by the persistence mechanism. This is a valid value for the HashMap.

Probably requires tweaking the Map persistence delegate.

Commit to 1.5.1. May escalate depending on need.
###@###.### 2003-09-16
                                     
2003-09-16



Hardware and Software, Engineered to Work Together