0

I have read other answers to try to find the solution but nothing clearly explains the issue.

Why should the following code block create a null pointer exception for currArraySize?

    int biggestArraySize = 0;
   int currArraySize = 0;

   for(int i = 0 ; i < myMap.keySet().size(); i++){

       ArrayList<String> currList = new ArrayList<String>();

      currList =  myMap.get(i);

      currArraySize = currList.size();

       if(currArraySize > biggestArraySize){

        biggestArraySize = currArraySize;

        }

    }


    System.out.println(biggestArraySize);
  • 1
    Clearly you have a null value somewhere in your set. – Joe C Sep 24 '17 at 18:14
  • 1
    `myMap.get(i)` is returning `null`, because `Integer.valueOf(i)` isn't in your map's keyset; to put it another way, your map's keys aren't the integers `0` to `myMap.keySet().size()-1`. If you want to iterate the values, use `for (ArrayList currList : myMap.values())`. – Andy Turner Sep 24 '17 at 18:15
  • 1
    Look at the answer to the duplicate question used to close this one, at the general steps used for debugging NullPointerExceptions (NPE's). You will see more of these, trust me, a **lot** more of them, and you'd best learn best practices on how to debug them now. – DontKnowMuchBut Getting Better Sep 24 '17 at 18:18

0 Answers0