2

I'm trying to fill the values of the hashmap

HashMap<Integer,List<Human>> hm=new HashMap<>(100);

for(int j=0;j<hm.size();j++)
{
    hm.put(j,new ArrayList<>());
}

But when i'm do that :

Human x= new Human();
hm.get(3).add(x);

I get:

java.lang.NullPointerException
2
  • 11
    hm.size() is not returning 100 but 0. 100 is the initialCapacitiy not the current size. See HashMap and HashMap.size(). Commented Mar 23, 2018 at 12:05
  • Please remember to check What should I do when someone answers my question?. It is better to validate the answer that fit the most your need to mark your question automatically to "answered". Commented Mar 26, 2018 at 11:40

3 Answers 3

4

hm.size() is zero - because you did not put there anything. 100 parameter is just initialization of the "start capacity" here.

Change your loop to for(int j=0;j<100;j++)

Sign up to request clarification or add additional context in comments.

Comments

2

You are not getting 100 with hm.size() but 0. You don't get what you have build with that constructor. You have set the "initialCapacitiy" with that constructor :

HashMap(int initialCapacity)

Constructs an empty HashMap with the specified initial capacity and the default load factor (0.75).

But HashMap.size() return the actual number of key-value element.

Returns the number of key-value mappings in this map.

For your information :

An instance of HashMap has two parameters that affect its performance: initial capacity and load factor. The capacity is the number of buckets in the hash table, and the initial capacity is simply the capacity at the time the hash table is created. The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased. When the number of entries in the hash table exceeds the product of the load factor and the current capacity, the hash table is rehashed (that is, internal data structures are rebuilt) so that the hash table has approximately twice the number of buckets.

Instead, simply set 100 in the loop to instanciate your List.

Map<Integer,List<Human>> hm = new HashMap<>();
for(int j=0; j < 100; j++){
    hm.put(j,new ArrayList<>());
}

Or even better, use a constant somewhere like

final static int NB_LIST = 100;

Map<Integer,List<Human>> hm = new HashMap<>();
for(int j=0; j < NB_LIST; j++){
    hm.put(j,new ArrayList<>());
}

Comments

1

hm.size() returns 0 because you still haven't added anything in the hashmap. From java site:

Returns the number of key-value mappings in this map.

You initialize it with some initial size but you have no values inside so your for cycle is not doing anything. If you do it for 1 to 100 instead of 1 to size it will work as you expect.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.