0
Map<String, String> listOfIndexes = INDEXED_TABLES.get(tableName);
Iterator it = listOfIndexes.entrySet().iterator();
while (it.hasNext()) { 
    Map.Entry pairs = (Map.Entry)it.next();
    System.out.println(pairs.getKey());
}

My Hashmap is like this :

public static  Map<String, Map<String, String>> INDEXED_TABLES = new HashMap<String, Map<String, String>>()
{{
    put("employee",  EMPLOYEE);
}};

public static  Map<String, String> EMPLOYEE = new HashMap<String, String>()
{{
    put("name", "Test");
    put("age", "Test");
    put("sex", "test");
}};
gran_profaci
  • 8,087
  • 15
  • 66
  • 99

2 Answers2

3

This is because you outsmarted yourself: your initializers depend on the order of execution. At the time this line runs

put("employee",  EMPLOYEE);

EMPLOYEE is still null, so that's what gets put into your Map<String,Map<String,String>>.

You can switch the order of initializers to fix this problem. However, you would be better if you put initialization code into a separate initializer, rather than using anonymous classes with custom initializers:

public static  Map<String, Map<String, String>> INDEXED_TABLES = new HashMap<String, Map<String, String>>();
public static  Map<String, String> EMPLOYEE = new HashMap<String, String>();
static {
    EMPLOYEE.put("name", "Test");
    EMPLOYEE.put("age", "Test");
    EMPLOYEE.put("sex", "test");
    INDEXED_TABLES.put("employee",  EMPLOYEE);
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
2

It looks like you are putting EMPLOYEE into the map before it has been initialized, so it will be null (and remain so, even if you assign something to EMPLOYEE later).

Reverse the order of the two statements.

Or, while in general I disapprove of the double-brace-initializer (hopefully, we'll get proper Collection literals in Java 8):

public static  Map<String, Map<String, String>> INDEXED_TABLES = 
  new HashMap<String, Map<String, String>>(){{
    put("employee",  new HashMap<String, String>(){{
      put("name", "Test");
      put("age", "Test");
      put("sex", "test");
   }}
}}
Community
  • 1
  • 1
Thilo
  • 257,207
  • 101
  • 511
  • 656
  • They are in different classes. And the second one for sure is instantiated before the call is made. Let me see this though. Thanks. – gran_profaci Jul 12 '13 at 02:30