Why the below code throws null pointer excetion in test.put(1,"sd")
public class HashMap {
@SuppressWarnings("null")
public static void main(String a[]){
Map test = null;
test.put(1, "sd");
System.out.println(test);
}
}
Why the below code throws null pointer excetion in test.put(1,"sd")
public class HashMap {
@SuppressWarnings("null")
public static void main(String a[]){
Map test = null;
test.put(1, "sd");
System.out.println(test);
}
}
Because test is null.
Map test = null;
Try instantiating the map instead.
Map test = new HashMap();
Or try a parameterized map:
Map<Integer,String> test = new HashMap<Integer,String>();