我在Util类中有一些HasMaps。都属于公共静态。我已经通过服务层实例化了它们。但问题是当我使用get方法检索键的值时,它返回null值。但是,如果我打印整个地图,它表明键和值都存在。
Util类:
public class Util_GetGeoCode
{
//divsion
// public static Map<Integer,String> map_division_id = new HashMap();
public static Map map_division_id = new HashMap();
//district
public static Map map_district_id = new HashMap();
//upzilla
public static Map map_upozila_id = new HashMap();
//union
public static Map map_uinon_id = new HashMap();
public static void clear_all_maps()
{
//test
System.out.println("clear Map");
map_division_id.clear();
map_district_id.clear();
map_upozila_id.clear();
map_uinon_id.clear();
}
}//class
将设置地图的代码:
public class BeneficiarySyncManager extends SyncManager
{
public void execute()
{
//other codes
setAll_locations_geoCode_by_id(moswBeneList);
System.out.println(" After setting Util_GetGeoCode.map_division_id : "+Util_GetGeoCode.map_division_id);
Map m = Util_GetGeoCode.map_division_id;
System.out.println(" m (key) : "+m.keySet());
System.out.println(" After get : "+Util_GetGeoCode.map_division_id.get(3));
//other codes
//clear all the maps of id to geo code
Util_GetGeoCode.clear_all_maps();
}
public void setAll_locations_geoCode_by_id(List list_moswBene)
{
//divsion
Set set_division_id = new HashSet();
//district
Set set_district_id = new HashSet();
//upzilla
Set set_upozila_id = new HashSet();
//union
Set set_uinon_id = new HashSet();
for (Iterator iterator = list_moswBene.iterator(); iterator.hasNext();)
{
Beneficiaries_Mosw next_Mosw = (Beneficiaries_Mosw) iterator.next();
set_division_id.add(next_Mosw.getPresentDivisionId());
set_division_id.add(next_Mosw.getPermanentDivisionId());
set_district_id.add(next_Mosw.getPermanentDistrictId());
set_district_id.add(next_Mosw.getPresentDistrictId());
set_upozila_id.add(next_Mosw.getPermanentThanaUpazilaId());
set_upozila_id.add(next_Mosw.getPresentThanaUpazilaId());
set_uinon_id.add(next_Mosw.getPermanentUnionWardId());
set_uinon_id.add(next_Mosw.getPresentUnionWardId());
}//for (Iterator iterator = set_uinon_id.iterator(); iterator.hasNext();)
Util_GetGeoCode.map_division_id = sManager.service_getGeoCode_Divisions(set_division_id);
Util_GetGeoCode.map_district_id = sManager.service_getGeoCode_District(set_district_id);
Util_GetGeoCode.map_upozila_id = sManager.service_getGeoCode_Thana_Upazilas(set_upozila_id);
Util_GetGeoCode.map_uinon_id = sManager.service_getGeoCode_union_wards(set_uinon_id);
//this line also prin null
System.out.println(" inside set : Util_GetGeoCode.map_division_id : "+Util_GetGeoCode.map_division_id.get(3));
}//public void setAll_locations_geoCode_by_id(List list_moswBene)
}//class
输出:
//other messages
inside set : Util_GetGeoCode.map_division_id : null
After setting Util_GetGeoCode.map_division_id : {3=30}
m (key) : [3]
After get : null
但是当我测试时,同样的get方法已经有效了。下面我提供了带输出的测试代码。
测试代码:
@Test
public void test_getGeoCode_of_divisions()
{
BeneficiaryDao beneficiaryDao = new BeneficiaryDao();
Set set_division_id = new HashSet();
set_division_id.add(1);
set_division_id.add(2);
set_division_id.add(3);
Util_GetGeoCode.map_division_id = beneficiaryDao.dao_getGeoCode_Divisions(set_division_id);
Map map_divGeoCodes = beneficiaryDao.dao_getGeoCode_Divisions(set_division_id);
System.out.println("\n\nGeoCode of Divs : "+map_divGeoCodes);
System.out.println(map_divGeoCodes.get(1));
System.out.println("Util_GetGeoCode.map_division_id : " +Util_GetGeoCode.map_division_id.get(3));
System.out.println("\n\n");
}//public void test_getGeoCode_of_divisions()
测试结果: Div的GeoCode:{1 = 10,2 = 20,3 = 30} 10 Util_GetGeoCode.map_division_id:30