Friday, August 9, 2013

Null pointer exception in HashMap

1.     Don’t forget to set the hash map before using it.

 Below code will through NullPointerException
import java.util.HashMap;

public class hashnull1 {
static HashMap<String,String>hm;

  public static void main(String[] args)
   {  
      hm.put("1","Apple");
      // the hashmap ‘hm’ is not defined
   }
}

When you run the above code it will through java.lang.NullPointerException.
In the above code we declare the HashMap hm but forgot to define it,
On declaration the HashMap will be having null value without any memory allocation.

HashMap<String,String>hm equal to HashMap<String,String>hm = null;

On the defining part only the memory allocation is happening to hm.

So we need to set hm before using this.
hm = new HashMap<String, String>();

Correct code:

public class hashnull1 {
       static HashMap<String,String>hm;
          public static void main(String[] args)
          {  
                 hm = new HashMap<String, String>();
              hm.put("1","Apple");
          }

}

2.      Do the null check before using HashMap.

 In the above example we can avoid null pointer exception if we might have done null check for hm.

public class hashnull1 {
       static HashMap<String,String>hm;
          public static void main(String[] args)
          {  
                if(hm != null) //checking for null, it can avoid nullpointer ex
              hm.put("1","Apple");
          }

}

3.      Do the Null check for values from the hash map

See the below code.
public class hashnull1 {
       static HashMap<String, String> hm;

       public static void main(String[] args) {
              hm = new HashMap<String, String>();
              String a = hm.get("2"); // value of ‘a’ will be ‘Null’ since key is not there in ‘hm’
              if(a.equals("apple")) { // Since ‘a’ is null it will lead ‘Null pointer’
                     System.out.println("Apple");
              }
       }

}

In the above code try to get the value for  key “2” which is not there in hm, it will always return “null”
Also HashMap will allow null values, so always do the null check for values from the HashMap

Corrected code:
public class hashnull1 {
       static HashMap<String, String> hm;

       public static void main(String[] args) {
              hm = new HashMap<String, String>();
              String a = hm.get("2");
              if(a != null && a.equals("apple")) {
                     System.out.println("Apple");
              }
       }

}
Null values are allowed in hashmap so do the null check for values
hm.put("1", null);
a = hm.get("1");
System.out.println("a>>>"+a); // print “null”

4.     Check for the key existence in hasmap using “containsKey”

map.containsKey(key)
In the above code we can also add a check for key existence in the “HashMap” 
public class hashnull1 {
       static HashMap<String, String> hm;

       public static void main(String[] args) {
              hm = new HashMap<String, String>();
              if (hm.containsKey("2")) { // check whether the key ‘2’ is present in the ‘hm’
                     String a = hm.get("2");
                     if (a != null && a.equals("apple")) {
// hm can contain null values so need to do the null check for values
                           System.out.println("Apple");
                     }
              }
       }

}
 


No comments:

Post a Comment