-1

I followed advice on constructing a static hashmap from other SO posts, but now I am having a null pointed exception when I try to access the information in the HashMap. Any ideas?

public final class EMUtil {
static private HashMap<String,TreeSet<String>> EMGroups;
static{
    HashMap<String,TreeSet<String>> EMGroups = new HashMap<String, TreeSet<String>>();

    TreeSet<String> temp=new TreeSet<String>();
    temp.addAll(returnArray("99201","99202","99203","99204","99205"));
    EMGroups.put("Set 1",temp);

    temp=new TreeSet<String>();
    temp.addAll(returnArray("99211","99212","99213","99214","99215"));
    EMGroups.put("Set 2",temp);

    ...
}

static public boolean isEM(String curCPT){
    if((curCPT.compareTo("99200")>0)&&(curCPT.compareTo("99311")<0)){
        for(TreeSet<String> value:EMGroups.values()){
            if(value.contains(curCPT)){
                return true;
            }
        }
    }
    return false;
}

Any ideas what is going on? If I am trying to have a group of sets that I access to check if a String is in a group within that set / which group it is in are there better ways to structure this?

mrfish
  • 64
  • 7
  • You're never using the field. – Jeroen Vannevel Apr 27 '15 at 16:44
  • 5
    You're shadowing your `EMGroups` variable in the static initializer. – Alexis C. Apr 27 '15 at 16:45
  • You have two different variables named EMGroups. One is a static member of the class which you never use anywhere, and the other is a local variable of the static block. Within the static block, the local variable shadows the field. – Solomon Slow Apr 27 '15 at 16:54
  • My fault there. I had tried to use a temporary "aMap" as shown in the accepted answer in http://stackoverflow.com/questions/507602/how-can-i-initialize-a-static-map. I was getting an ExceptionInInitializerError trying to run that, and somewhere in the middle I must have tried to undo work and created the copy. – mrfish Apr 27 '15 at 16:57
  • Paste the null pointer exception stack. – Mohan Raj Apr 27 '15 at 17:05

1 Answers1

3
static{
    HashMap<String,TreeSet<String>> EMGroups = new HashMap<String, TreeSet<String>>();

This variable declaration is hiding your static EMGroups class member. Should be:

static{
    EMUtil.EMGroups = new HashMap<String, TreeSet<String>>();

Though you can actually drop the 'EMUtil.'

Solomon Slow
  • 25,130
  • 5
  • 37
  • 57
ControlAltDel
  • 33,923
  • 10
  • 53
  • 80